# 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)]
59
我今天遇到了这个问题并在我想检查之前推出了自己的解决方案 . 这个:
dict((i,a.count(i)) for i in a)
对于大型列表来说真的非常慢 . 我的解决方案
def occurDict(items):
d = {}
for i in items:
if i in d:
d[i] = d[i]+1
else:
d[i] = 1
return d
实际上比Counter解决方案快一点,至少对于Python 2.7来说 .
11
以下是三种解决方案:
Fastest is using a for loop and storing it in a 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))
Result
#Solution 1 - 更快
{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
sum([1 for elem in <yourlist> if elem==<your_value>])
这将返回your_value的出现次数
1
Counting the occurrences of one item in a list
要计算只有一个列表项的出现次数,您可以使用 count()
>>> l = ["a","b","b"]
>>> l.count("a")
1
>>> l.count("b")
2
计算列表中所有项目的出现次数也称为"tallying"列表,或创建计数器计数器 .
Counting all items with count()
要计算 l 中项目的出现次数,可以简单地使用列表推导和 count() 方法
[[x,l.count(x)] for x in set(l)]
(或类似于字典 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}
Counting all items with Counter()
或者, collections 库中的 Counter 类更快
Counter(l)
例:
>>> l = ["a","b","b"]
>>> from collections import Counter
>>> Counter(l)
Counter({'b': 2, 'a': 1})
How much faster is Counter?
我检查了 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)
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
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)
22 回答
在字典中获取每个项目出现次数的另一种方法:
我今天遇到了这个问题并在我想检查之前推出了自己的解决方案 . 这个:
对于大型列表来说真的非常慢 . 我的解决方案
实际上比Counter解决方案快一点,至少对于Python 2.7来说 .
以下是三种解决方案:
Fastest is using a for loop and storing it in a Dict.
Result
您还可以使用内置模块operator的countOf方法 .
list.count(x)
返回x
出现在列表中的次数见:http://docs.python.org/tutorial/datastructures.html#more-on-lists
这将返回your_value的出现次数
Counting the occurrences of one item in a list
要计算只有一个列表项的出现次数,您可以使用
count()
计算列表中所有项目的出现次数也称为"tallying"列表,或创建计数器计数器 .
Counting all items with count()
要计算
l
中项目的出现次数,可以简单地使用列表推导和count()
方法(或类似于字典
dict((x,l.count(x)) for x in set(l))
)例:
Counting all items with Counter()
或者,
collections
库中的Counter
类更快例:
How much faster is Counter?
我检查了
Counter
用于计算列表的速度有多快 . 我用n
的几个值尝试了两种方法,并且看起来Counter
的常数因子大约为2 .这是我用过的脚本:
并输出:
如果您只想要一个项目的计数,请使用
count
方法:Don't 如果要计算多个项目,请使用此项 . 在循环中调用
count
需要在每个count
调用的列表上单独传递,这对性能来说可能是灾难性的 . 如果要计算所有项目,或者甚至只计算多个项目,请使用Counter
,如其他答案中所述 .为什么不使用熊猫?
输出:
如果您正在寻找特定元素的计数,请说:a,尝试:
输出:
使用itertools.groupby()计算所有元素的数量
用于获取列表中所有元素计数的Antoher可能性可以通过
itertools.groupby()
.With "duplicate" counts
返回
注意它如何将前三个
a
组合为第一组,而a
的其他组则位于列表的下方 . 发生这种情况是因为输入列表L
未排序 . 如果这些团体实际上应该是分开的,那么这有时会带来好处 .With unique counts
如果需要唯一的组计数,只需对输入列表进行排序:
返回
这是一个示例列表:
list.count
有
list.count
方法这适用于任何列表 . 元组也有这种方法:
collections.Counter
然后就是馆藏 . 计数器 . 您可以将任何iterable转储到Counter中,而不仅仅是列表,Counter将保留元素计数的数据结构 .
用法:
计数器基于Python字典,它们的键是元素,因此键需要是可清除的 . 它们基本上就像允许冗余元素进入它们的集合 .
collections.Counter的进一步使用
您可以使用计数器中的可迭代添加或减去:
您也可以使用计数器进行多组操作:
为什么不是熊猫?
另一个答案暗示:
Pandas是一个常见的库,但它不在标准库中 . 将其添加为要求并非易事 .
在列表对象本身以及标准库中有针对此用例的内置解决方案 .
如果你的项目还不需要pandas,那么仅仅为了这个功能而要求它是愚蠢的 .
如果您想要特定元素的多次出现:
如果你想 count all values at once 你可以使用numpy数组和
bincount
非常快地完成它,如下所示这使
建议使用numpy的bincount,但它仅适用于具有非负整数的1d数组 . 此外,生成的数组可能会令人困惑(它包含从原始列表的min到max的整数出现,并将缺少的整数设置为0) .
使用numpy更好的方法是使用unique函数,并将属性
return_counts
设置为True . 它返回一个元组,其中包含唯一值的数组和每个唯一值的出现数组 .然后我们可以将它们配对
它也适用于其他数据类型和“2d列出“,例如
要计算具有共同类型的不同元素的数量:
给
3
,而不是6我已经将所有建议的解决方案(以及一些新解决方案)与perfplot(我的一个小项目)进行了比较 .
计算一个项目
对于足够大的数组,事实证明
比其他解决方案略快 .
计算所有项目
As established before,
是你想要的 .
重现情节的代码:
2 .
可能不是最有效的,需要额外的通过来删除重复 .
功能实施:
回报:
或者返回
dict
:回报:
如果您可以使用
pandas
,那么value_counts
就可以进行救援了 .它还会根据频率自动对结果进行排序 .
如果您希望结果位于列表列表中,请执行以下操作
输出清单:
如果您使用的是Python 2.7或3,并且您希望每个元素出现次数: