python - 在Python中找出具有一定长度的列表的数量

标签 python

给定以下列表

arrayNumbers = [[32,3154,53,13],[44,34,25,67], [687,346,75], [57,154]]

如何有效地获取只有 4 个项目的列表数?

在这种情况下,这将是 arrayNumbers_len = 2。我可以使用循环来执行此操作,但这根本没有效率。由于我的真实数组的长度以百万计,我需要一种方法来非常快地完成这项工作。

这是我目前的解决方案:

batchSize = 4
counter = 0
for i in range(len(arrayNumbers)):
    if (len(arrayNumbers[i]) == batchSize):
        counter += 1

有什么建议吗?

最佳答案

我继续做了一些计时来展示这些不同的方法有何不同。

注意:这些在 Python 3 中:

注意:var_arr 有一百万个随机大小的子列表:

In [31]: def for_loop(var_arr, batchsize):
    ...:     count = 0
    ...:     for x in var_arr:
    ...:         if len(x) == batchsize:
    ...:             count += 1
    ...:     return count
    ...:

In [32]: def with_map_count(var_arr, batchsize):
    ...:     return list(map(len, var_arr)).count(batchsize)
    ...:

In [33]: def lambda_filter(var_arr, batchsize):
    ...:     len(list(filter(lambda l: len(l) == batchsize, var_arr)))
    ...:

In [34]: def sum_gen(var_arr, batchsize):
    ...:     sum(len(x) == batchsize for x in var_arr)
    ...:

In [35]: from collections import Counter
    ...: def with_counter(var_arr, batchsize):
    ...:     Counter(map(len, var_arr)).get(batchsize, 0)
    ...:

In [36]: %timeit for_loop(var_arr, 4)
82.9 ms ± 1.23 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [37]: %timeit with_map_count(var_arr, 4)
48 ms ± 873 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [38]: %timeit lambda_filter(var_arr, 4)
172 ms ± 3.76 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [39]: %timeit sum_gen(var_arr, 4)
150 ms ± 3.12 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [40]: %timeit with_counter(var_arr, 4)
75.6 ms ± 1.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

更多时间:

In [50]: def with_list_comp_filter(var_arr, batchsize):
    ...:     return len([x for x in var_arr if len(x) == batchsize])
    ...:
    ...: def with_list_comp_filter_map(var_arr, batchsize):
    ...:     return len([x for x in map(len, var_arr) if x == batchsize])
    ...:
    ...: def loop_with_map(var_arr, batchsize):
    ...:     count = 0
    ...:     for x in map(len, var_arr):
    ...:         count += x == batchsize
    ...:     return count
    ...:

In [51]: %timeit with_list_comp_filter(var_arr, 4)
87.8 ms ± 4.35 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [52]: %timeit with_list_comp_filter_map(var_arr, 4)
62.7 ms ± 1.63 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

In [53]: %timeit loop_with_map(var_arr, 4)
91.9 ms ± 1.43 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

关于python - 在Python中找出具有一定长度的列表的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49036501/

相关文章:

python - 从 shell 运行 python 时如何避免打印不必要的信息?

python - numpy 数组作为结构化数组中的数据类型?

python - Pip 安装 : can't open file pip, 或父模块 '' 未加载

python - opencv如何通过跟踪栏缩放图像?

python - 在排序的、固定大小的列表/python 中运行前 k 个元素

GCE 上的 Python : connection failed because connected host has failed to respond

python - 选择节点和边形成具有属性的 networkx 图

python - 在 Python 中除以长整型会返回错误的答案

python - 使用 Mercurial 忽略 manage.py (hg .hgignore)

python - 如何使用 Enum.__members__ 无法访问的私有(private)枚举值?