【问题标题】:Python print statement not printing anything at the end of my functionPython print 语句在我的函数结束时不打印任何内容
【发布时间】:2018-11-02 22:17:04
【问题描述】:

我想在函数结束时打印一个短语,但我想要的输出没有打印。 python中没有弹出错误,它只是没有打印并且表现得好像它忽略了它。 wordlist 是用户输入的单词列表,用于查找每个单词在他们输入的网站中出现的次数。 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()

【问题讨论】:

  • 你可能应该在某个地方打电话给count(....,...)......你不在这里。
  • 你想在这里解决什么问题?计算 wordlist 中所有单词出现在 sitewordlist 中的频率?
  • 你仍然没有在任何地方打电话给count(.......) - 并且不要显示你的数据是什么样的(wordlistsitewordlist)。

标签: python python-3.x function printing python-3.6


【解决方案1】:

问题:在您的第一个while 中,您增加x 直到等于len(wordlist) - 只有当x 小于len(wordlist) 时才输入您的第二个while - 这有点矛盾。


您可以使用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})

虽然在这里不太合适。您可以使用普通 dict 模拟 Counter ,而像这样:

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 ... 与普通/默认字典一起使用:

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)

输出相同。

【讨论】:

  • 有没有办法在没有计数器的情况下计算一个单词在文本中出现的次数?我对这个项目有一定的要求,所以我尝试使用一个while循环来满足一些要求。我只是尝试将它拆分为两个不同的函数,从概念上讲我的代码是有意义的,但是 python 仍然没有打印它,所以我不确定我的基本原理在哪里是错误的。我会发布一个更新的版本。
  • @NickieD 如果您有“某些要求”,那么您的项目就是家庭作业。在这里使用一段时间是不好的——它会导致你对一个列表进行索引——你可以简单地使用for element in text.lower().split() 来遍历所有单词,witch is much better。使用 Counter 会更好。如果 Counter 本身有问题,请使用普通 dict,检查每个单词是否已经在您的 dict 中,如果没有添加,则计数为 1。如果它在您的 dict 中,则将其计数增加 1。为所有分词。您可以使用collection.defaultdict(int) 来避免检查。这里有很多类似的Q。
  • 使用一个列表来存储一个单词,如果它还没有在其中,另一个列表存储(在同一索引处)你遇到的数字是 bad 因为它导致多个完整列表扫描以查看您当前的单词是否在一个列表中,如果是则获取索引,否则附加它,然后在第二个列表中增加索引或附加 1 .. 这在计算上比使用 dict 更糟糕我不会给你代码。编程意味着知道何时使用什么数据结构和列表以及索引到列表中只是糟糕在这里。
【解决方案2】:

您正在使用 while 循环来像 for 循环一样工作,但是您在两者中都使用了相同的迭代器 x,并且您没有在两者之间将其值重置为 0。所以第二个while循环看到x已经等于len(wordlist),所以它不会执行循环体。

【讨论】:

    猜你喜欢
    • 2018-07-06
    • 2012-06-29
    • 1970-01-01
    • 2022-01-09
    • 2013-07-19
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多