【问题标题】:counting the number of letters in each word of a paragraph python计算段落python每个单词中的字母数
【发布时间】:2020-11-16 07:27:28
【问题描述】:

如果我需要编写一个函数来读取一个大段落并打印出段落中每个长度有多少个单词,我该怎么做?

这是我迄今为止尝试过的。

#split the piece of writing into a list so I can search over every word
#need to find out how to make this not take so long

piece = "hello world"
words = piece.split()

#search over every word, have a variable for each number, check to see length of word and add correspondingly

for word in range(words):
    one = 0
    two = 0
    three = 0
    four = 0
    five = 0
    six = 0
    seven = 0
    eight = 0
    nine = 0
    ten = 0
    eleven = 0
    twelve = 0
    thirteen = 0
    other = 0
    total = 0
    if (len(word) == 1):
        one += 1
        total += 1
    elif (len(word) == 2):
        two += 1
        total += 1
    elif (len(word) == 3):
        three += 1
        total += 1
    elif (len(word) == 4):
        four += 1
        total += 1
    elif (len(word) == 5):
        five += 1
        total += 1
    elif (len(word) == 6):
        six += 1
        total += 1
    elif (len(word) == 7):
        seven += 1
        total += 1
    elif (len(word) == 8):
        eight += 1
        total += 1
    elif (len(word) == 9):
        nine += 1
        total += 1
    elif (len(word) == 10):
        ten += 1
        total += 1
    elif (len(word) == 11):
        eleven += 1
        total += 1
    elif (len(word) == 12):
        twelve += 1
        total += 1
    elif (len(word) == 13):
        thirteen += 1
        total += 1
    else:
        other += 1
        total += 1

#print results
print(f'Proportion of 1- letter words: {one / total * 100}% {one} words')
print(f'Proportion of 2- letter words: {two / total* 100}% {two} words')
print(f'Proportion of 3- letter words: {three / total* 100}% {three} words')
print(f'Proportion of 4- letter words: {four / total * 100}% {four} words')
print(f'Proportion of 5- letter words: {five / total * 100}% {five} words')
print(f'Proportion of 6- letter words: {six / total * 100}% {six} words')
print(f'Proportion of 7- letter words: {seven/ total * 100}% {seven} words')
print(f'Proportion of 8- letter words: {eight / total * 100}% {eight} words')
print(f'Proportion of 9- letter words: {nine / total * 100}% {nine} words')
print(f'Proportion of 10- letter words: {ten / total * 100}% {ten} words')
print(f'Proportion of 11- letter words: {eleven / total * 100}% {eleven} words')
print(f'Proportion of 12- letter words: {twelve / total * 100}% {twelve} words')
print(f'Proportion of 13- letter words: {thirteen / total * 100}% {thirteen} words')

我认为这两个问题是我不知道如何让循环在段落的整个长度内运行,而且我不知道如何编写代码,以便使用大量文本不需要永远运行。

【问题讨论】:

    标签: python list counting word letter


    【解决方案1】:
    • 尽量避免重复代码。例如,使用字典(例如stats)而不是多个变量更容易,在每个单词上增加其记录(stats[len(word)] +=
    • Python 中包含大量电池,可以大大减少您编写的代码量。在这种情况下,defaultdictCounter 可能会有所帮助。

    应用这些后,你会得到类似的东西:

    from collections import Counter
    
    stats = Counter(len(word) for word in paragraph.split())
    total_words = sum(stats.values())
    
    for length in sorted(stats.keys()):
        print("proportions of %d words: %f" % (length, stats[length] / total_words))
    

    UPD: 旁注:在迭代字典时,Python 仅使用键。 Counter 来自字典的子类,因此它具有相同的行为。因此,为简洁起见,只需 for length in sorted(stats): 即可,但对于不熟悉此 Python 功能的人来说,它可能看起来不直观。 stats.keys() 导致相同的结果,但更明确。

    【讨论】:

    • 我从来不知道这个计数器。谢谢你教我一些新东西!
    【解决方案2】:

    你好,代码统计所有字母、逗号等。

    fname = input('Enter the file name: ')
    try:
        f=open(fname)
    except:
        print('The file can not be opened')
    t=[]
    for line in f:
        s=line.split()
        t.extend(s)
    h=dict()
    for i in t:
        for j in I:
            if j in h:
                h[j]+=1
            else:
                h[j]=1
    print(h)
     
    

    【讨论】:

    • 我会把你好从你的标题中去掉,并包含一些关于你正在做什么的背景信息。照原样,这个答案有点简洁。
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 1970-01-01
    • 2016-05-18
    • 2023-02-11
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    相关资源
    最近更新 更多