docs.python.org/3.8/library/collections.html
collections.
Counter
([iterable-or-mapping])¶
A counter tool is provided to support convenient and rapid tallies.
from collections import Counter
cnt = Counter()
L = ['red', 'blue', 'red', 'green', 'blue', 'blue'] for e in L:
cnt[e] += 1
print(cnt)
Counter({'blue': 3, 'red': 2, 'green': 1})
Counter puo' essere inizializzato
con la stessa espressione che ha come suo print
Counter({'blue': 3, 'red': 2, 'green': 1})
the key is not inserted (with the value of 0)
cnt['a'] # 'a' not a key
0
'a' in cnt # 'a' not inserted
False
from collections import Counter
import re words = re.findall(r'\w+', open('pinocchio.txt').read().lower()) Counter(words).most_common(20)
one_recur = [] # create a void list
for e in Counter(words).most_common():
if e[1] == 1:
one_recur.append(e[0])
L = Counter(words).most_common()
[e[0] for e in L if e[1] == 1]
syntax
[f(x) for x in y if g(x)]