How to count a sequence of characters and map characters to counts in Python

2 Answers

0 votes
import collections

print(collections.Counter(['a', 'b', 'a', 'a', 'c', 'b', 'b', 'b']))



'''
run:

Counter({'b': 4, 'a': 3, 'c': 1})

'''

 



answered May 5, 2019 by avibootz
edited May 5, 2019 by avibootz
0 votes
import collections

c = collections.Counter(['a', 'b', 'a', 'a', 'c', 'b', 'b', 'b'])

print(c['a'])
print(c['b'])
print(c['c'])



'''
run:

3
4
1

'''

 



answered May 5, 2019 by avibootz
...