Łuk*_*asz 1730
如果您只想要一个项目的计数,请使用以下count
方法:
>>> [1, 2, 3, 4, 1, 4, 1].count(1)
3
Run Code Online (Sandbox Code Playgroud)
如果要计算多个项目,请不要使用此项.count
在循环中调用需要在每个count
调用的列表上单独传递,这对性能来说可能是灾难性的.如果您想要计算所有项目,甚至只计算多个项目,请使用Counter
,如其他答案中所述.
use*_*778 1627
如果您使用的是Python 2.7或3,并且您希望每个元素出现次数:
>>> from collections import Counter
>>> z = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
>>> Counter(z)
Counter({'blue': 3, 'red': 2, 'yellow': 1})
Run Code Online (Sandbox Code Playgroud)
use*_*737 245
计算列表中一个项目的出现次数
用于计算您可以使用的仅一个列表项的出现次数 count()
>>> l = ["a","b","b"]
>>> l.count("a")
1
>>> l.count("b")
2
Run Code Online (Sandbox Code Playgroud)
计算列表中所有项目的出现次数也称为"统计"列表,或创建计数器计数器.
使用count()计算所有项目
要计算l
一个项目的出现次数,只需使用列表推导和count()
方法即可
[[x,l.count(x)] for x in set(l)]
Run Code Online (Sandbox Code Playgroud)
(或类似于字典dict((x,l.count(x)) for x in set(l))
)
例:
>>> l = ["a","b","b"]
>>> [[x,l.count(x)] for x in set(l)]
[['a', 1], ['b', 2]]
>>> dict((x,l.count(x)) for x in set(l))
{'a': 1, 'b': 2}
Run Code Online (Sandbox Code Playgroud)
使用Counter()计算所有项目
或者,库中的Counter
类更快collections
Counter(l)
Run Code Online (Sandbox Code Playgroud)
例:
>>> l = ["a","b","b"]
>>> from collections import Counter
>>> Counter(l)
Counter({'b': 2, 'a': 1})
Run Code Online (Sandbox Code Playgroud)
计数器的速度有多快?
我检查了Counter
计算列表的速度有多快.我用两个值尝试了两种方法,n
并且看起来Counter
通过大约2的常数因子更快.
这是我使用的脚本:
from __future__ import print_function
import timeit
t1=timeit.Timer('Counter(l)', \
'import random;import string;from collections import Counter;n=1000;l=[random.choice(string.ascii_letters) for x in range(n)]'
)
t2=timeit.Timer('[[x,l.count(x)] for x in set(l)]',
'import random;import string;n=1000;l=[random.choice(string.ascii_letters) for x in range(n)]'
)
print("Counter(): ", t1.repeat(repeat=3,number=10000))
print("count(): ", t2.repeat(repeat=3,number=10000)
Run Code Online (Sandbox Code Playgroud)
并输出:
Counter(): [0.46062711701961234, 0.4022796869976446, 0.3974247490405105]
count(): [7.779430688009597, 7.962715800967999, 8.420845870045014]
Run Code Online (Sandbox Code Playgroud)
小智 63
另一种在字典中获取每个项目出现次数的方法:
dict((i, a.count(i)) for i in a)
Run Code Online (Sandbox Code Playgroud)
Sil*_*rom 44
list.count(x)
返回x
列表中显示的次数
请参阅:http: //docs.python.org/tutorial/datastructures.html#more-on-lists
Aar*_*all 42
给定一个项目,如何在Python的列表中计算它的出现次数?
这是一个示例列表:
>>> l = list('aaaaabbbbcccdde')
>>> l
['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']
Run Code Online (Sandbox Code Playgroud)
list.count
有list.count
方法
>>> l.count('b')
4
Run Code Online (Sandbox Code Playgroud)
这适用于任何列表.元组也有这种方法:
>>> t = tuple('aabbbffffff')
>>> t
('a', 'a', 'b', 'b', 'b', 'f', 'f', 'f', 'f', 'f', 'f')
>>> t.count('f')
6
Run Code Online (Sandbox Code Playgroud)
collections.Counter
然后是收藏品.计数器.您可以将任何可迭代转储到计数器中,而不仅仅是列表,并且计数器将保留元素计数的数据结构.
用法:
>>> from collections import Counter
>>> c = Counter(l)
>>> c['b']
4
Run Code Online (Sandbox Code Playgroud)
计数器基于Python字典,它们的键是元素,因此键需要是可清除的.它们基本上就像允许冗余元素进入它们的集合.
collections.Counter
您可以使用计数器中的iterables添加或减去:
>>> c.update(list('bbb'))
>>> c['b']
7
>>> c.subtract(list('bbb'))
>>> c['b']
4
Run Code Online (Sandbox Code Playgroud)
您也可以使用计数器进行多组操作:
>>> c2 = Counter(list('aabbxyz'))
>>> c - c2 # set difference
Counter({'a': 3, 'c': 3, 'b': 2, 'd': 2, 'e': 1})
>>> c + c2 # addition of all elements
Counter({'a': 7, 'b': 6, 'c': 3, 'd': 2, 'e': 1, 'y': 1, 'x': 1, 'z': 1})
>>> c | c2 # set union
Counter({'a': 5, 'b': 4, 'c': 3, 'd': 2, 'e': 1, 'y': 1, 'x': 1, 'z': 1})
>>> c & c2 # set intersection
Counter({'a': 2, 'b': 2})
Run Code Online (Sandbox Code Playgroud)
另一个答案暗示:
为什么不用熊猫?
Pandas是一个常见的库,但它不在标准库中.将其添加为要求并非易事.
在列表对象本身以及标准库中有针对此用例的内置解决方案.
如果您的项目不需要pandas,那么仅仅为此功能提供它是愚蠢的.
flo*_*onk 34
如果要一次计算所有值,可以使用numpy数组快速完成bincount
,如下所示
import numpy as np
a = np.array([1, 2, 3, 4, 1, 4, 1])
np.bincount(a)
Run Code Online (Sandbox Code Playgroud)
这使
>>> array([0, 3, 1, 1, 2])
Run Code Online (Sandbox Code Playgroud)
Nic*_*mer 33
我已经将所有建议的解决方案(以及一些新解决方案)与perfplot(我的一个小项目)进行了比较.
对于足够大的数组,事实证明
numpy.sum(numpy.array(a) == 1)
Run Code Online (Sandbox Code Playgroud)
比其他解决方案略快.
如前所述,
numpy.bincount(a)
Run Code Online (Sandbox Code Playgroud)
是你想要的.
重现图的代码:
from collections import Counter
from collections import defaultdict
import numpy
import operator
import pandas
import perfplot
def counter(a):
return Counter(a)
def count(a):
return dict((i, a.count(i)) for i in set(a))
def bincount(a):
return numpy.bincount(a)
def pandas_value_counts(a):
return pandas.Series(a).value_counts()
def occur_dict(a):
d = {}
for i in a:
if i in d:
d[i] = d[i]+1
else:
d[i] = 1
return d
def count_unsorted_list_items(items):
counts = defaultdict(int)
for item in items:
counts[item] += 1
return dict(counts)
def operator_countof(a):
return dict((i, operator.countOf(a, i)) for i in set(a))
perfplot.show(
setup=lambda n: list(numpy.random.randint(0, 100, n)),
n_range=[2**k for k in range(20)],
kernels=[
counter, count, bincount, pandas_value_counts, occur_dict,
count_unsorted_list_items, operator_countof
],
equality_check=None,
logx=True,
logy=True,
)
Run Code Online (Sandbox Code Playgroud)
2.
from collections import Counter
from collections import defaultdict
import numpy
import operator
import pandas
import perfplot
def counter(a):
return Counter(a)
def count(a):
return dict((i, a.count(i)) for i in set(a))
def bincount(a):
return numpy.bincount(a)
def pandas_value_counts(a):
return pandas.Series(a).value_counts()
def occur_dict(a):
d = {}
for i in a:
if i in d:
d[i] = d[i]+1
else:
d[i] = 1
return d
def count_unsorted_list_items(items):
counts = defaultdict(int)
for item in items:
counts[item] += 1
return dict(counts)
def operator_countof(a):
return dict((i, operator.countOf(a, i)) for i in set(a))
perfplot.show(
setup=lambda n: list(numpy.random.randint(0, 100, n)),
n_range=[2**k for k in range(20)],
kernels=[
counter, count, bincount, pandas_value_counts, occur_dict,
count_unsorted_list_items, operator_countof
],
equality_check=None,
logx=True,
logy=True,
)
Run Code Online (Sandbox Code Playgroud)
Thi*_*vel 19
如果你可以使用pandas
,那value_counts
就是救援.
>>> import pandas as pd
>>> a = [1, 2, 3, 4, 1, 4, 1]
>>> pd.Series(a).value_counts()
1 3
4 2
3 1
2 1
dtype: int64
Run Code Online (Sandbox Code Playgroud)
它还会根据频率自动对结果进行排序.
如果您希望结果位于列表列表中,请执行以下操作
>>> pd.Series(a).value_counts().reset_index().values.tolist()
[[1, 3], [4, 2], [3, 1], [2, 1]]
Run Code Online (Sandbox Code Playgroud)
Sho*_*esh 14
为什么不使用熊猫?
import pandas as pd
l = ['a', 'b', 'c', 'd', 'a', 'd', 'a']
# converting the list to a Series and counting the values
my_count = pd.Series(l).value_counts()
my_count
Run Code Online (Sandbox Code Playgroud)
输出:
a 3
d 2
b 1
c 1
dtype: int64
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找特定元素的计数,请说:a,尝试:
my_count['a']
Run Code Online (Sandbox Code Playgroud)
输出:
3
Run Code Online (Sandbox Code Playgroud)
小智 13
我今天遇到了这个问题并且在我想要检查之前推出了我自己的解决方案.这个:
dict((i,a.count(i)) for i in a)
Run Code Online (Sandbox Code Playgroud)
对于大型列表来说真的非常慢.我的解决方案
def occurDict(items):
d = {}
for i in items:
if i in d:
d[i] = d[i]+1
else:
d[i] = 1
return d
Run Code Online (Sandbox Code Playgroud)
实际上比Counter解决方案快一点,至少对于Python 2.7来说.
Wes*_*ner 12
# Python >= 2.6 (defaultdict) && < 2.7 (Counter, OrderedDict)
from collections import defaultdict
def count_unsorted_list_items(items):
"""
:param items: iterable of hashable items to count
:type items: iterable
:returns: dict of counts like Py2.7 Counter
:rtype: dict
"""
counts = defaultdict(int)
for item in items:
counts[item] += 1
return dict(counts)
# Python >= 2.2 (generators)
def count_sorted_list_items(items):
"""
:param items: sorted iterable of items to count
:type items: sorted iterable
:returns: generator of (item, count) tuples
:rtype: generator
"""
if not items:
return
elif len(items) == 1:
yield (items[0], 1)
return
prev_item = items[0]
count = 1
for item in items[1:]:
if prev_item == item:
count += 1
else:
yield (prev_item, count)
count = 1
prev_item = item
yield (item, count)
return
import unittest
class TestListCounters(unittest.TestCase):
def test_count_unsorted_list_items(self):
D = (
([], []),
([2], [(2,1)]),
([2,2], [(2,2)]),
([2,2,2,2,3,3,5,5], [(2,4), (3,2), (5,2)]),
)
for inp, exp_outp in D:
counts = count_unsorted_list_items(inp)
print inp, exp_outp, counts
self.assertEqual(counts, dict( exp_outp ))
inp, exp_outp = UNSORTED_WIN = ([2,2,4,2], [(2,3), (4,1)])
self.assertEqual(dict( exp_outp ), count_unsorted_list_items(inp) )
def test_count_sorted_list_items(self):
D = (
([], []),
([2], [(2,1)]),
([2,2], [(2,2)]),
([2,2,2,2,3,3,5,5], [(2,4), (3,2), (5,2)]),
)
for inp, exp_outp in D:
counts = list( count_sorted_list_items(inp) )
print inp, exp_outp, counts
self.assertEqual(counts, exp_outp)
inp, exp_outp = UNSORTED_FAIL = ([2,2,4,2], [(2,3), (4,1)])
self.assertEqual(exp_outp, list( count_sorted_list_items(inp) ))
# ... [(2,2), (4,1), (2,1)]
Run Code Online (Sandbox Code Playgroud)
Har*_*cha 11
虽然这是一个很老的问题,但由于我没有找到一个班轮,我做了一个。
# original numbers in list
l = [1, 2, 2, 3, 3, 3, 4]
# empty dictionary to hold pair of number and its count
d = {}
# loop through all elements and store count
[ d.update( {i:d.get(i, 0)+1} ) for i in l ]
print(d)
# {1: 1, 2: 2, 3: 3, 4: 1}
Run Code Online (Sandbox Code Playgroud)
itertools.groupby()
获取列表中所有元素的计数的另一种可能性是使用itertools.groupby()
。
具有“重复”计数
from itertools import groupby
L = ['a', 'a', 'a', 't', 'q', 'a', 'd', 'a', 'd', 'c'] # Input list
counts = [(i, len(list(c))) for i,c in groupby(L)] # Create value-count pairs as list of tuples
print(counts)
Run Code Online (Sandbox Code Playgroud)
退货
[('a', 3), ('t', 1), ('q', 1), ('a', 1), ('d', 1), ('a', 1), ('d', 1), ('c', 1)]
Run Code Online (Sandbox Code Playgroud)
请注意,它是如何将前三个组合在一起a
作为第一组的,而其他组合a
则位于列表的下方。发生这种情况是因为未对输入列表L
进行排序。如果组实际上应该分开,那么有时这可能是一个好处。
具有独特的计数
如果需要唯一的组计数,只需对输入列表进行排序:
counts = [(i, len(list(c))) for i,c in groupby(sorted(L))]
print(counts)
Run Code Online (Sandbox Code Playgroud)
退货
[('a', 5), ('c', 1), ('d', 2), ('q', 1), ('t', 1)]
Run Code Online (Sandbox Code Playgroud)
注意:为了创建唯一计数,与groupby
解决方案相比,许多其他答案都提供了更轻松,更易读的代码。但是这里显示它与重复计数示例相似。
要计算具有共同类型的不同元素的数量:
li = ['A0','c5','A8','A2','A5','c2','A3','A9']
print sum(1 for el in li if el[0]=='A' and el[1] in '01234')
Run Code Online (Sandbox Code Playgroud)
给
3
而不是6
>>> import operator
>>> operator.countOf([1, 2, 3, 4, 1, 4, 1], 1)
3
Run Code Online (Sandbox Code Playgroud)
建议使用numpy的bincount,但是它仅适用于具有非负整数的一维数组。而且,结果数组可能会造成混淆(它包含原始列表的从最小值到最大值的整数的出现,并将丢失的整数设置为0)。
使用numpy更好的方法是使用属性设置为True 的唯一函数return_counts
。它返回一个元组,该元组具有唯一值的数组和每个唯一值的出现的数组。
# a = [1, 1, 0, 2, 1, 0, 3, 3]
a_uniq, counts = np.unique(a, return_counts=True) # array([0, 1, 2, 3]), array([2, 3, 1, 2]
Run Code Online (Sandbox Code Playgroud)
然后我们可以将它们配对为
dict(zip(a_uniq, counts)) # {0: 2, 1: 3, 2: 1, 3: 2}
Run Code Online (Sandbox Code Playgroud)
它还可以与其他数据类型和“ 2d列表”一起使用,例如
>>> a = [['a', 'b', 'b', 'b'], ['a', 'c', 'c', 'a']]
>>> dict(zip(*np.unique(a, return_counts=True)))
{'a': 3, 'b': 3, 'c': 2}
Run Code Online (Sandbox Code Playgroud)
最快的是使用for循环并将其存储在Dict中。
import time
from collections import Counter
def countElement(a):
g = {}
for i in a:
if i in g:
g[i] +=1
else:
g[i] =1
return g
z = [1,1,1,1,2,2,2,2,3,3,4,5,5,234,23,3,12,3,123,12,31,23,13,2,4,23,42,42,34,234,23,42,34,23,423,42,34,23,423,4,234,23,42,34,23,4,23,423,4,23,4]
#Solution 1 - Faster
st = time.monotonic()
for i in range(1000000):
b = countElement(z)
et = time.monotonic()
print(b)
print('Simple for loop and storing it in dict - Duration: {}'.format(et - st))
#Solution 2 - Fast
st = time.monotonic()
for i in range(1000000):
a = Counter(z)
et = time.monotonic()
print (a)
print('Using collections.Counter - Duration: {}'.format(et - st))
#Solution 3 - Slow
st = time.monotonic()
for i in range(1000000):
g = dict([(i, z.count(i)) for i in set(z)])
et = time.monotonic()
print(g)
print('Using list comprehension - Duration: {}'.format(et - st))
Run Code Online (Sandbox Code Playgroud)
结果
Run Code Online (Sandbox Code Playgroud)#Solution 1 - Faster
{1: 4, 2: 5, 3: 4, 4: 6, 5: 2, 234: 3, 23: 10, 12: 2, 123: 1, 31: 1, 13: 1, 42: 5, 34: 4, 423: 3}
Simple for loop and storing it in dict - Duration: 12.032000000000153
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)#Solution 2 - Fast
Counter({23: 10, 4: 6, 2: 5, 42: 5, 1: 4, 3: 4, 34: 4, 234: 3, 423: 3, 5: 2, 12: 2, 123: 1, 31: 1, 13: 1})
Using collections.Counter - Duration: 15.889999999999418
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)#Solution 3 - Slow
{1: 4, 2: 5, 3: 4, 4: 6, 5: 2, 34: 4, 423: 3, 234: 3, 42: 5, 12: 2, 13: 1, 23: 10, 123: 1, 31: 1}
Using list comprehension - Duration: 33.0
Run Code Online (Sandbox Code Playgroud)
小智 5
我会使用filter()
,以卢卡斯的例子为例:
>>> lst = [1, 2, 3, 4, 1, 4, 1]
>>> len(filter(lambda x: x==1, lst))
3
Run Code Online (Sandbox Code Playgroud)
使用 %timeit 查看哪个操作更有效。np.array 计数操作应该更快。
from collections import Counter
mylist = [1,7,7,7,3,9,9,9,7,9,10,0]
types_counts=Counter(mylist)
print(types_counts)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1511397 次 |
最近记录: |