简体   繁体   中英

Counting the occurrences of a character in each element of a list

     lst = ['10010001','01101001']

我需要计算每个元素中数字“1”出现的次数

for element in lst:
    print element.count("1")
>>> lst = ['10010001','01101001']
>>> [l.count('1') for l in lst]
[3, 4]
from collections import Counter
num_of_1s = [Counter(item)['1'] for item in lst]

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