python 序列中出现次数最多的元素

问题

怎样找出一个序列中出现次数最多的元素呢?

实现

通过 collections.Counter 类来实现上述需求, 示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>>> from collections import Counter

>>> words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
>>> word_counts = Counter(words)
>>> word_counts
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2, 'not': 1, "don't": 1, "you're": 1, 'under': 1})

# 出现频率最高的3个单词
>>> word_counts.most_common(3)
[('eyes', 8), ('the', 5), ('look', 4)]

说明

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
>>> from collections import Counter

# 统计words中单词出现频率
>>> word_counts = Counter('hello world')
>>> word_counts
Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})

# word_counts 示例中的方法
>>> dir(word_counts)
['_keep_positive', 'clear', 'copy', 'elements', 'fromkeys', 'get', 'items', 'keys', 'most_common', 'pop', 'popitem', 'setdefault', 'subtract', 'update', 'values']

# word_counts.copy()
Return a shallow copy.
>>> word_counts.copy()
Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})

# word_counts.elements()
Iterator over elements repeating each as many times as its count.
>>> word_counts.elements()
<itertools.chain object at 0x000002516523E5F8>
>>> list(word_counts.elements())
['h', 'e', 'l', 'l', 'l', 'o', 'o', ' ', 'w', 'r', 'd']

# word_counts.most_common(n=None)
List the n most common elements and their counts from the most common to the least.
If n is None, then list all element counts
>>> word_counts.most_common(n=2)
[('l', 3), ('o', 2)]

# word_counts.subtract(*args, **kwds)
Like dict.update() but subtracts counts instead of replacing them.
Counts can be reduced below zero. Both the inputs and outputs are
allowed to contain zero and negative counts.
>>> word_counts = Counter('hello world')
>>> word_counts.subtract('ll ooo')
>>> word_counts
Counter({'h': 1, 'e': 1, 'l': 1, 'w': 1, 'r': 1, 'd': 1, 'o': -1, ' ': -1})

# word_counts.update(*args, **kwds)
Like dict.update() but add counts instead of replacing them.
>>> word_counts.update('ll ooo')
>>> word_counts
Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, 'w': 1, 'r': 1, 'd': 1, ' ': 0})

# word_counts.clear()
Remove all items from D.

# word_counts.get(key, default=None)
Return the value for key if key is in the dictionary, else default.

# word_counts.items()
a set-like object providing a view on word_counts's items

# word_counts.keys()
a set-like object providing a view on word_counts's keys

# word_counts.pop(k[,d])
v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised

# word_counts.popitem()
(k, v), remove and return some (key, value) pair as a 2-tuple; but raise KeyError if word_counts is empty.

# word_counts.etdefault(key, default=None)
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.

# word_counts.values()
an object providing a view on D's values
-------------本文结束感谢您的阅读-------------