zoukankan      html  css  js  c++  java
  • 用Python统计列表中出现一次以上的数

    >>> 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.

    原来如此。

  • 相关阅读:
    Permutations II
    N-Queens II
    Palindrome Number
    Minimum Path Sum
    JS的DOM操作2
    JS 的DOM操作
    函数概念
    JavaScript数组
    JavaScript循环及练习
    JS语言
  • 原文地址:https://www.cnblogs.com/wangshide/p/2228295.html
Copyright © 2011-2022 走看看