计算列表中每个元素中字符的出现次数

时间:2018-02-18 15:36:29

标签: python list parity

     lst = ['10010001','01101001']

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

3 个答案:

答案 0 :(得分:0)

for element in lst:
    print element.count("1")

答案 1 :(得分:0)

>>> lst = ['10010001','01101001']
>>> [l.count('1') for l in lst]
[3, 4]

答案 2 :(得分:-1)

from collections import Counter
num_of_1s = [Counter(item)['1'] for item in lst]