counter in python
>>> from collections import Counter
>>>
>>> myList = [1,1,2,3,4,5,3,2,3,4,2,1,2,3]
>>> print Counter(myList)
Counter({2: 4, 3: 4, 1: 3, 4: 2, 5: 1})
>>>
>>> print Counter(myList).items()
[(1, 3), (2, 4), (3, 4), (4, 2), (5, 1)]
>>>
>>> print Counter(myList).keys()
[1, 2, 3, 4, 5]
>>>
>>> print Counter(myList).values()
[3, 4, 4, 2, 1]
5
2
$ python collections_counter_init.py
Counter({'b': 3, 'a': 2, 'c': 1})
Counter({'b': 3, 'a': 2, 'c': 1})
Counter({'b': 3, 'a': 2, 'c': 1})
Thank you!
2
0
4.22
9
import collections
print collections.Counter(['a', 'b', 'c', 'a', 'b', 'b'])
print collections.Counter({'a':2, 'b':3, 'c':1})
print collections.Counter(a=2, b=3, c=1)
Thank you!
9
0
4
5
# import Counter from collections
from collections import Counter
# creating a raw data-set using keyword arguments
x = Counter (a = 2, x = 3, b = 3, z = 1, y = 5, c = 0, d = -3)
# printing out the elements
for i in x.elements():
print( "% s : % s" % (i, x[i]), end ="\n")
Thank you!
5
0
3.2
5
from collections import Counter
list1 = ['x','y','z','x','x','x','y','z']
print(Counter(list1))
Thank you!
5
0
3.89
9
from collections import Counter
strl = "aabbaba"
print(Counter(str1))
Counter({'a': 4, 'b': 3})
Thank you!
9
0
Are there any code examples left?
New code examples in category Python