from collections import Counter
type(Counter)
type类型,本身是type类型。
例如:
- txt=“from collections import Counter”
count=Counter(text)
Counter({‘o’: 5, ‘r’: 3, ’ ': 3, ‘t’: 3, ‘m’: 2, ‘c’: 2, ‘l’: 2, ‘e’: 2, ‘i’: 2, ‘n’: 2, ‘f’: 1, ‘s’: 1, ‘p’: 1, ‘C’: 1, ‘u’: 1})
- txt=“from collections import Counter”
count=Counter(text.split())
Counter({‘from’: 1, ‘collections’: 1, ‘import’: 1, ‘Counter’: 1})
- txt=“from collections import Counter”
count=Counter(txt)
print(count.most_common())
[(‘o’, 5), (‘r’, 3), (’ ', 3), (‘t’, 3), (‘m’, 2), (‘c’, 2), (‘l’, 2), (‘e’, 2), (‘i’, 2), (‘n’, 2), (‘f’, 1), (‘s’, 1), (‘p’, 1), (‘C’, 1), (‘u’, 1)]
4.txt=“from collections import Counter” (排名前两名的)
count=Counter(txt)
print(count.most_common(2))
[(‘o’, 5), (‘r’, 3)]