代码:计算一个list里面各个值的个数

方法1:

from collections import Counter

list1=['apple','egg','apple','banana','egg','apple']

counts = Counter(list1)

print(counts)

# Counter({'apple': 3, 'egg': 2, 'banana': 1})

方法2:

words = file("test.txt", "r").read().split() #read the words into a list.

uniqWords = sorted(set(words)) #remove duplicate words and sort for word in uniqWords:

print words.count(word), word

你可能感兴趣的:(代码:计算一个list里面各个值的个数)