from collections import Counter
fruits = ["apple", "banana", "orange", "apple", "banana", "apple"] fruit_counter = Counter(fruits) print(fruit_counter) # Output: Counter({'apple': 3, 'banana': 2, 'orange': 1}) print(fruit_counter["apple"]) # Output: 3 print(fruit_counter["banana"]) # Output: 2
Counter({'apple': 3, 'banana': 2, 'orange': 1}) 3 2
text = "Hello, world!" char_counter = Counter(text) print(char_counter) # Output: Counter({'l': 3, 'o': 2, 'H': 1, 'e': 1, ',': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1, '!': 1}) print(char_counter["l"]) # Output: 3 print(char_counter["o"]) # Output: 2
Counter({'l': 3, 'o': 2, 'H': 1, 'e': 1, ',': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1, '!': 1}) 3 2