Заявление печати Python ничего не печатает в конце моей функции - PullRequest
0 голосов
/ 03 ноября 2018

Я хочу напечатать фразу в конце своей функции, но желаемый результат не печатается. В python не появляется никаких ошибок, он просто не печатает и ведет себя так, как будто игнорирует его. список слов - это список слов, введенных пользователем, чтобы узнать, сколько раз каждое слово появляется на веб-сайте, который они ввели. sitewordlist - это полный список слов на сайте.

def count(wordlist, sitewordlist):
    x = 0
    while x < len(wordlist):
       numblist = []
       wordcount = sitewordlist.count(wordlist[x])
       numblist.append(wordcount)
       x = x + 1
    final(numblist, wordlist)

def final(numblist, wordlist):    
    y = 0
    while y < len(numblist):
    print("The word" + wordlist[y] + "appears" + numblist[y] + "times.")
    y = y + 1
main()

Ответы [ 2 ]

0 голосов
/ 03 ноября 2018

Проблема: в вашем первом while вы увеличиваете x до тех пор, пока оно не станет равным len(wordlist) - ваш второй while вводится только в том случае, если x меньше len(wordlist) - это противоречиво.


Вы можете использовать collections.Counter, чтобы легко считать вещи и получать от них подсказку:

from collections import Counter
def count(wordlist, sitewordlist):
    data = Counter(sitewordlist)

    for w in wordlist:
        print(f"The word {w} appears {data.get(w,0)} times.")

text = """n 1066, William of Normandy introduced what, in later centuries, became referred
to as a feudal system, by which he sought the advice of a council of tenants-in-chief (a 
person who held land) and ecclesiastics before making laws. In 1215, the tenants-in-chief 
secured Magna Carta from King John, which established that the king may not levy or collect
any taxes (except the feudal taxes to which they were hitherto accustomed), save with the 
consent of his royal council, which gradually developed into a parliament. Over the 
centuries, the English Parliament progressively limited the power of the English monarchy 
which arguably culminated in the English Civil War and the trial and execution of Charles 
I in 1649. After the restoration of the monarchy under Charles II, and the subsequent 
Glorious Revolution of 1688, the supremacy of Parliament was a settled principle and all 
future English and later British sovereigns were restricted to the role of constitutional 
monarchs with limited executive authority. The Act of Union 1707 merged the English 
Parliament with the Parliament of Scotland to form the Parliament of Great Britain. 
When the Parliament of Ireland was abolished in 1801, its former members were merged 
into what was now called the Parliament of the United Kingdom. 
(quote from: https://en.wikipedia.org/wiki/Parliament_of_England)""".split()

# some cleanup
text[:] = [t.strip(".,-!?1234567890)([]{}\n") for t in text]
words = ["is","and","not","are"]

count(words,text)

Выход:

The word is appears 0 times.
The word and appears 6 times.
The word not appears 1 times.
The word are appears 0 times.

Полный счетчик:

Counter({'the': 22, 'of': 15, 'Parliament': 7, '': 6, 'and': 6, 'a': 5, 'which': 5,
'English': 5, 'in': 4, 'to': 4, 'were': 3, 'with': 3, 'was': 3, 'what': 2, 'later': 2,
'centuries': 2, 'feudal': 2, 'council': 2, 'tenants-in-chief': 2, 'taxes': 2, 'into': 2,
'limited': 2,'monarchy': 2, 'Charles': 2, 'merged': 2, 'n': 1, 'William': 1, 'Normandy': 1,
'introduced': 1, 'became': 1, 'referred': 1, 'as': 1, 'system': 1, 'by': 1, 'he': 1,
'sought': 1, 'advice': 1, 'person': 1, 'who': 1, 'held': 1, 'land': 1, 'ecclesiastics': 1, 
'before': 1, 'making': 1, 'laws': 1, 'In': 1, 'secured': 1, 'Magna': 1, 'Carta': 1,
'from': 1, 'King': 1, 'John': 1, 'established': 1, 'that': 1, 'king': 1, 'may': 1,
'not': 1, 'levy': 1, 'or': 1, 'collect': 1, 'any': 1, 'except': 1, 'they': 1, 
'hitherto': 1, 'accustomed': 1, 'save': 1, 'consent': 1, 'his': 1, 'royal': 1, 
'gradually': 1, 'developed': 1, 'parliament': 1, 'Over': 1, 'progressively': 1, 'power': 1,
'arguably': 1, 'culminated': 1, 'Civil': 1, 'War': 1, 'trial': 1, 'execution': 1, 
'I': 1, 'After': 1, 'restoration': 1, 'under': 1, 'II': 1, 'subsequent': 1, 'Glorious': 1,
'Revolution': 1, 'supremacy': 1, 'settled': 1, 'principle': 1, 'all': 1, 'future': 1, 
'British': 1, 'sovereigns': 1, 'restricted': 1, 'role': 1, 'constitutional': 1, 
'monarchs': 1, 'executive': 1, 'authority': 1, 'The': 1, 'Act': 1, 'Union': 1, 
'Scotland': 1, 'form': 1, 'Great': 1, 'Britain': 1, 'When': 1, 'Ireland': 1, 
'abolished': 1, 'its': 1, 'former': 1, 'members': 1, 'now': 1, 'called': 1, 'United': 1, 
'Kingdom': 1, 'quote': 1, 'from:': 1, 
'https://en.wikipedia.org/wiki/Parliament_of_England': 1})

Пока здесь не совсем уместно. Вы можете смоделировать Counter используя обычный dict и, в то же время, сделайте так:

def count_me_other(words,text):
    wordlist = words.split()
    splitted = (x.strip(".,!?") for x in text.split())
    d = {}
    it = iter(splitted)
    try:
        while it:
            c =  next(it)
            if c not in d:
                d[c]=1
            else:
                d[c]+=1
    except StopIteration:
        for w in wordlist:
            print(f"The word {w} appears {d.get(w,0)} times.")

wordlist = "A C E G I K M" 
text = "A B C D E F G A B C D E F A B C D E A B C D A B C A B A"

count_me_other(wordlist,text)

Выход:

The word A appears 7 times.
The word C appears 5 times.
The word E appears 3 times.
The word G appears 1 times.
The word I appears 0 times.
The word K appears 0 times.
The word M appears 0 times.

Или используйте for ... в сочетании с нормальным / defaultdict:

def count_me_other_2(words,text):
    wordlist = words.split()
    splitted = (x.strip(".,!?") for x in text.split())
    d = {}
    for w in splitted:
        if w not in d:
            d[w]=1
        else:
            d[w]+=1
    for w in wordlist:
        print(f"The word {w} appears {d.get(w,0)} times.")

def count_me_other_3(words,text):
    from collections import defaultdict            
    wordlist = words.split()
    splitted = (x.strip(".,!?") for x in text.split())
    d = defaultdict(int)
    for w in splitted:
        d[w] += 1
    for w in wordlist:
        print(f"The word {w} appears {d.get(w,0)} times.")


count_me_other_2(wordlist,text)
count_me_other_3(wordlist,text)

с одинаковым выходом.

0 голосов
/ 03 ноября 2018

Вы используете while-циклы, чтобы действовать как for-loop, но вы используете один и тот же итератор x в обоих, и вы не сбрасываете его значение в 0 между ними. Таким образом, второй цикл while видит, что x уже равен len(wordlist), и поэтому он не выполняет тело цикла.

...