from collections import Counter()

Application demonstration of Counter():

from collections import Counter
list_01 = ['A','C','S','A','B','f','S','A']
dict_01 = Counter(list_01)
print(dict_01)

Will directly output a dictionary, the content is the elements in the list and their frequency of occurrence:

Counter({
    
    'A': 3, 'S': 2, 'C': 1, 'B': 1, 'f': 1})

In addition, Counter() can be instantiated:

total_counts = Counter()
print(type(total_counts))

The type of total_counts is displayed as Class:

<class 'collections.Counter'>

After instantiation, it can be used for further traversal in the following form:

for word,freq in total_counts:
	if freq>1:
		print(word)

Guess you like

Origin blog.csdn.net/weixin_45281949/article/details/103094987