读取文件并打印出python中包含1,2,3和4个字母的单词数

时间:2015-10-12 00:01:37

标签: python file printing words

我试过了,

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。任何帮助表示赞赏!

2 个答案:

答案 0 :(得分:2)

你的问题是你总是为每个单词递增计数:

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))

答案 1 :(得分:0)

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

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]