我有一个像这样的列表
L=['d','f','d','c','c','f','d','f']
我想知道L中有多少d,f和c出现并存储结果如下:
R=[['d',3],['f',3],['c',2]]
什么是最好的approch(算法)?
答案 0 :(得分:5)
最好的方法(算法),就是不要自己动手!
>>> from collections import Counter
>>> L=['d','f','d','c','c','f','d','f']
>>> Counter(L)
Counter({'d': 3, 'f': 3, 'c': 2})
如果你坚持列表:
>>> Counter(L).items()
[('c', 2), ('d', 3), ('f', 3)]
答案 1 :(得分:2)
我觉得字典对此更好:
>>> from collections import Counter
>>> L = ['d','f','d','c','c','f','d','f']
>>> Counter(L)
Counter({'d': 3, 'f': 3, 'c': 2})
但是,如果你坚持列表清单:
>>> L=['d','f','d','c','c','f','d','f']
>>> from collections import Counter
>>> var = Counter(L)
>>> [[key, value] for key, value in var.items()]
[['c', 2], ['d', 3], ['f', 3]]
答案 2 :(得分:1)
L=['d','f','d','c','c','f','d','f']
from collections import Counter
print Counter(L)
<强>输出强>
Counter({'d': 3, 'f': 3, 'c': 2})
您可以使用Counter.most_common
方法获取此结果
print Counter(L).most_common()
<强>输出强>
[('d', 3), ('f', 3), ('c', 2)]
答案 3 :(得分:1)
在排序数据上使用itertools.groupby的可能解决方案
<强>实施强>
from itertools import groupby
[[k, len(list(v))] for k, v in groupby(sorted(L))]
<强>输出强>
[['c', 2], ['d', 3], ['f', 3]]
效果比较
In [9]: L = [choice(ascii_letters) for _ in range(1000)]
In [10]: %timeit [[k, len(list(v))] for k, v in groupby(sorted(L))]
1000 loops, best of 3: 271 us per loop
In [11]: %timeit Counter(L).items()
1000 loops, best of 3: 306 us per loop
注意的
应该注意的是,计数器解决方案中散列数据的开销超过了Tim's Sort