Contact: aviboots(AT)netvision.net.il
41,578 questions
54,200 answers
573 users
from collections import Counter occurrences = Counter() for word in ['aaa', 'bbb', 'bbb', 'ccc', 'aaa', 'aaa', 'ddd']: occurrences[word] += 1 print(occurrences) ''' run: Counter({'aaa': 3, 'bbb': 2, 'ddd': 1, 'ccc': 1}) '''
from collections import Counter lst = ['aaa', 'bbb', 'bbb', 'ccc', 'aaa', 'aaa', 'ddd'] occurrences = Counter() for word in lst: occurrences[word] += 1 for item in occurrences.items(): print(item[0], ":", item[1]) ''' run: aaa : 3 ccc : 1 bbb : 2 ddd : 1 '''