如何计算文本文件中的元素?

时间:2017-07-27 15:38:54

标签: python

我正在尝试计算文本文件中的元素。我知道我错过了一个明显的部分,但我不能把手指放在上面。这就是我目前所拥有的只是产生字母“f”而非文件的数量:

<% if current_user.try(:username) == "admin" || current_user == element.user %>

1 个答案:

答案 0 :(得分:2)

import collections

counts = collections.Counter()  # create a new counter
with open(filename) as infile:  # open the file for reading
    for line in filename: 
        for number in line.split(): 
            counts.update((number,))
            print("Now there are {} instances of {}".format(counts[number], number))
print(counts)