[英]How to find number of instances of an item in a python list
我希望腳本的一部分是這樣的。
if list[1] is in list.pop n times:
return True
只需使用:
list.count(element)
例:
>>> [1,2,3,4,2,1].count(1)
2
列表中的項目數:
len(myList)
第i
個元素在列表中出現的次數:
myList.count(mylist[i])
或者您可以使用Counter
:
>>> from collections import Counter
>>> Counter([1, 2, 3, 1, 1])
Counter({1: 3, 2: 1, 3: 1})
>>>
如果您想一次獲取列表中不同元素的所有計數,則這很好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.