How to use counter to count the letters from a string in Python

2 Answers

0 votes
from collections import Counter

counter = Counter()
for ch in "python pro":
    counter[ch] += 1

print(counter.most_common())


'''
run:

[('p', 2), ('o', 2), ('y', 1), ('t', 1), ('h', 1), ('n', 1), (' ', 1), ('r', 1)]
 
'''

 



answered Jul 26, 2019 by avibootz
0 votes
from collections import Counter

counter = Counter()
for ch in "python pro":
    counter[ch] += 1
    
lst = counter.most_common()

for item in lst:
    print(item[0], item[1])



'''
run:

p 2
o 2
y 1
t 1
h 1
n 1
  1
r 1
 
'''

 



answered Jul 26, 2019 by avibootz
...