Fastest way to count number of occurrences in a Python list

前端 未结 5 1981
悲&欢浪女
悲&欢浪女 2020-11-30 03:29

I have a Python list and I want to know what\'s the quickest way to count the number of occurrences of the item, \'1\' in this list. In my actual case, the item

5条回答
  •  臣服心动
    2020-11-30 04:30

    Combination of lambda and map function can also do the job:

    list_ = ['a', 'b', 'b', 'c']
    sum(map(lambda x: x=="b", list_))
    :2
    

提交回复
热议问题