[英]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. joined
加入所有的字符为一个字符串"2343422799"
然后,我们通过数字环路0-9
,并使用count
方法来查看每个数字出现的次数串并打印数字和使用count字符串格式化。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.