【问题标题】:Counting the occurrences of a character in each element of a list计算一个字符在列表的每个元素中出现的次数
【发布时间】:2018-07-28 22:25:02
【问题描述】:
     lst = ['10010001','01101001']

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

【问题讨论】:

  • 你试过什么?你在哪里卡住?你想要的输出是什么?

标签: python list parity


【解决方案1】:
>>> lst = ['10010001','01101001']
>>> [l.count('1') for l in lst]
[3, 4]

【讨论】:

    【解决方案2】:
    from collections import Counter
    num_of_1s = [Counter(item)['1'] for item in lst]
    

    【讨论】:

      【解决方案3】:
      for element in lst:
          print element.count("1")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多