This function returns the element occurrence n many times:
def cold_compress():
l = int(input())
inp_list = []
num_list = []
for lines in range(l):
b = input()
inp_list.append(b)
print(b)
for item in inp_list:
for x in item:
print(item.count(x))
For example: if my input is:
eeewwww
33jjji
...it will output:
3
3
3
4
4
4
4
2
2
3
3
3
1
How do I avoid this?
You can use Counter() to count the elements inside a list
Sample code:
from collections import Counter
listr = ["one","two","three","three","three","three",]
print(dict(Counter(listr)))
OUTPUT
{'one': 1, 'two': 1, 'three': 4}
Implementing the Counter() in your code:
from collections import Counter
def cold_compress():
listr = list(input())
print(dict(Counter(listr)))
cold_compress()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.