0

I am loading numbers from a text file into a list, and in that regard, everything works fine! But now I need to know how many times each number occured in the list. Below is my whole program that I have pieced together by searching this site.

row = []  
textfile = open('take5_3.txt', 'r')
yourResult = [line.split('-') for line in textfile]
row.append(yourResult)    
print (yourResult)    

Any time I put some sort of line that is suppose to count my result, I get one because it is only counting the list and not the items within the list.

4

2 回答 2

2

正如乔兰所评论的那样,您的问题确实不清楚。我将尝试在这里填写空白。

textfile = open('take5_3.txt', 'r')
yourResult = [line.split('-') for line in textfile.readlines()] # use readline to read from the file
# You probably need to flatten the content in yourResult.
# Assume now yourResult is something like this ['a', 'a', 'bdbd', 'bbc', 'bbc']
# you can use Counter to do the counting
from collections import Counter
print Counter(yourResult)

这是输出

Counter({'a': 2, 'bbc': 2, 'bdbd': 1})
于 2013-11-01T03:31:56.710 回答
0

您需要制作一个字典,其中数字为键,数字的计数为值。随着你的前进,不断增加价值。

于 2013-11-01T03:26:08.043 回答