简体   繁体   中英

Find all the occurrences of a character in a given list of string in python language

this is example what i'm looking for.

Example:

Input: ['234','34','22','7','99'] 

0:0
1:0
2:3
3:2
4:2
5:0
6:0
7:1
8:0
9:2

joined joins all the chars into one string "2343422799" Then we loop through the digits 0-9 and use the count method to see how many times each digit appears in the string and print the digit and the count using string formatting.

 l=['234','34','22','7','99']     
joined="".join(l) # joins all the chars into one string "2343422799"
for ch in range(10): # go through digits from 0-9
    print "{}:{}".format(ch,joined.count(str(ch))) 
0:0
1:0
2:3
3:2
4:2
5:0
6:0
7:1
8:0
9:2

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM