Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python列表计数方法的性能_Python_Performance - Fatal编程技术网

python列表计数方法的性能

python列表计数方法的性能,python,performance,Python,Performance,在python中,很容易获得列表中特定项的计数: >>> L = [1,1,1,2,3,4] >>> print(L.count(1)) 3 这是O(N)方法吗?是否建议使用此方法,或者是否有更好的方法快速获取列表中任意数量元素的计数(即避免O(mN)的方法,其中m是需要调用count的记录数)。是,如果需要通过iterable进行多次计数,请使用。那么您只需要迭代一次 >>> from collections import Counter

在python中,很容易获得列表中特定项的计数:

>>> L = [1,1,1,2,3,4]
>>> print(L.count(1))
3
这是O(N)方法吗?是否建议使用此方法,或者是否有更好的方法快速获取列表中任意数量元素的计数(即避免O(mN)的方法,其中m是需要调用
count
的记录数)。

是,如果需要通过iterable进行多次计数,请使用。那么您只需要迭代一次

>>> from collections import Counter
>>> counter = Counter('hellooo i am a potato!!!!!')
>>> counter.most_common(2)
[('o', 5), ('!', 5)]