您的位置:首页 > 编程语言 > Python开发

用Python统计列表中出现一次以上的数

2019-08-02 11:58 956 查看
原文链接:https://www.geek-share.com/detail/2530821380.html

 

>>> import collections

>>> a = list(range(1000000))
>>> a[100] = 1 #稍微改变一下列表
#方法一
>>> b = filter(lambda x: a.count(x) > 1, a)
#方法二
>>> d = filter(lambda x: x[1] != 1,collections.Counter(a).items())

为什么方法一要比方法二慢得多呢?

方法一中的count()函数要O(n^2)的时间复杂度。

方法二加速的原因是什么呢?到底是怎么实现的?(值得深究)

帮助文档:

Dict subclass for counting hashable items. Sometimes called a bag
or multiset. Elements are stored as dictionary keys and their counts
are stored as dictionary values.

原来如此。

转载于:https://www.cnblogs.com/wangshide/archive/2011/10/29/2228295.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: