2013-07-18 29 views

回答

6

只需使用:

list.count(element) 

實施例:

>>> [1,2,3,4,2,1].count(1) 
2 
2

列表中的項目的數量:

len(myList) 

的次數在一個發生i th元素列表:

myList.count(mylist[i]) 
2

或者你可以使用Counter

>>> from collections import Counter 
>>> Counter([1, 2, 3, 1, 1]) 
Counter({1: 3, 2: 1, 3: 1}) 
>>> 

,如果你想獲得列表中的一個時間不同元素的種種罪狀這是好事。

+0

這可能是一個很好的解決方案,但你可能想要擴展OP如何使用它來做他想做的事情。 – arshajii

相關問題