繁体   English   中英

读取文件并在python中打印出带有1、2、3和4个字母的单词数

[英]Read a file and print out the number of words with 1, 2, 3, and 4 letter in python

我试过了

def many(filename):
'prints the number of words of length 1, 2, 3, and 4 in file filename'
    infile = open(filename)
    content = infile.read()
    infile.close()

    words = content.split()
    count = 0

    for word in words:
        count += ((len(word) == 1))
        print("Words of length 1: {}".format(str(count)))

        count += (len(word) == 2)
        print("Words of length 2: {}".format(str(count)))

        count += (len(word) == 3)
        print("Words of length 3: {}".format(str(count)))

        count += (len(word) == 4)
        print("Words of length 4: {}".format(str(count)))

但是输出仅循环打印语句15次,打印0-15。 任何帮助表示赞赏!

您的问题是您总是对每个单词递增计数:

for each word
    if the word is of length 1:
        increment count
    if the word is of length 2:
        increment count
    if the word is of length 3:
        increment count
    if the word is of length 4:
        increment count

实际上,您想根据单词的长度增加不同的计数器。 一种方法是维护四个单独的计数器:

counter1 = 0
counter2 = 0
counter3 = 0
counter4 = 0

for word in words:
    if len(word) == 1:
        counter1 += 1
    if len(word) == 2:
        counter2 += 1
    if len(word) == 3:
        counter3 += 1
    if len(word) == 4:
        counter4 += 1

当然,当您要跟踪更多长度的单词时,这变得很混乱(例如:“计算长度为1 ... 20的单词的数量”将需要您维护20个变量。想象一下,如果转20个单词会发生什么情况)入100!)

正如另一个用户指出的那样,维护数组是执行此操作的最简单方法(您实际上也可以使用字典来执行此操作):

counts = [0, 0, 0, 0]
for word in words:
    wordLen = len(word)
    countIndex = wordLen - 1  # remember, python indexes lists from 0
    counts[coundIndex] += 1

for i in range(len(counts)):
    print("There are", countIndex[i], "words of length", i+1)  # again, adjusting for that 0-indexing behavior

如果您想使代码更简洁:

longestWordLength = 4
counts = [0]*(longestWordLength+1)
for word in words:
    counts[len(word)] += 1
for length, count in enumerate(counts):
    print("There are {} words of length {}".format(count, length))

一个更可爱的选择:

import collections

def many(filename):
    with open(filename) as infile:
        counts = collections.Counter(len(word) for line in infile for word in line.split())
    for length, count in counts.items():
        print("There are {} words of length {}".format(count, length))

您要做的是检查单词的长度并将其存储在某个位置,然后在最后打印结果。 当前,您正在遍历每一行并每次都打印一些内容。 您可以将其存储在这样的数组中:

count = [0] * 4
for word in words:
    length = len(word);
    count[length-1] = count[length-1] + 1

for x in range(4):
    wl = x + 1
    print "Words of length ", wl, ": ", count[x]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM