python - 获得列表联合的最快方法 - Python

标签 python list numpy set union

有一个 C++ 比较可以从列表的列表中获取列表的并集:The fastest way to find union of sets

还有其他几个与 python 相关的问题,但没有一个建议联合列表的最快方法:

从答案中,我了解到至少有两种方法可以做到这一点:

>>> from itertools import chain
>>> x = [[1,2,3], [3,4,5], [1,7,8]]
>>> list(set().union(*x))
[1, 2, 3, 4, 5, 7, 8]
>>> list(set(chain(*x)))
[1, 2, 3, 4, 5, 7, 8]

请注意,我之后将集合转换为列表,因为我需要固定列表的顺序以进行进一步处理。

经过一番比较,似乎list(set(chain(*x)))更稳定,耗时更少:

from itertools import chain
import time
import random

# Dry run.
x = [[random.choice(range(10000)) 
    for i in range(10)] for j in range(10)]
list(set().union(*x))
list(set(chain(*x)))

y_time = 0
z_time = 0

for _ in range(1000):
    x = [[random.choice(range(10000)) 
        for i in range(10)] for j in range(10)]
    start = time.time()
    y = list(set().union(*x))
    y_time += time.time() - start 
    #print 'list(set().union(*x)):\t', y_time
    start = time.time()
    z = list(set(chain(*x)))
    z_time += time.time() - start 
    #print 'list(set(chain(*x))):\t', z_time
    assert sorted(y) == sorted(z)
    #print 

print y_time / 1000.
print z_time / 1000. 

[输出]:

1.39586925507e-05
1.09834671021e-05

取出casting sets的变量列表:

y_time = 0
z_time = 0

for _ in range(1000):
    x = [[random.choice(range(10000)) 
        for i in range(10)] for j in range(10)]

    start = time.time()
    y = set().union(*x)
    y_time += time.time() - start 

    start = time.time()
    z = set(chain(*x))
    z_time += time.time() - start 

    assert sorted(y) == sorted(z)

print y_time / 1000.
print z_time / 1000. 

[输出]:

1.22241973877e-05
1.02684497833e-05

这是我尝试打印中间时间(没有列表转换)时的完整输出:http://pastebin.com/raw/y3i6dXZ8

为什么 list(set(chain(*x)))list(set().union(*x)) 花费的时间少>?

是否有另一种方法可以实现相同的列表并集?使用 numpypandassframe 或其他东西? 替代方法更快吗?

最佳答案

最快的取决于 x 的性质——它是长列表还是短列表,有很多子列表还是很少的子列表,子列表是长的还是短的,以及是否有许多重复或很少重复。

这里有一些 timeit 结果比较了一些替代方案。可能性如此之多,因此这绝不是一个完整的分析,但也许这将为您提供一个研究用例的框架。

func                 | x                    | time
unique_concatenate   | many_uniques         | 0.863
empty_set_union      | many_uniques         | 1.191
short_set_union_rest | many_uniques         | 1.192
long_set_union_rest  | many_uniques         | 1.194
set_chain            | many_uniques         | 1.224

func                 | x                    | time
long_set_union_rest  | many_duplicates      | 0.958
short_set_union_rest | many_duplicates      | 0.969
empty_set_union      | many_duplicates      | 0.971
set_chain            | many_duplicates      | 1.128
unique_concatenate   | many_duplicates      | 2.411

func                 | x                    | time
empty_set_union      | many_small_lists     | 1.023
long_set_union_rest  | many_small_lists     | 1.028
set_chain            | many_small_lists     | 1.032
short_set_union_rest | many_small_lists     | 1.036
unique_concatenate   | many_small_lists     | 1.351

func                 | x                    | time
long_set_union_rest  | few_large_lists      | 0.791
empty_set_union      | few_large_lists      | 0.813
unique_concatenate   | few_large_lists      | 0.814
set_chain            | few_large_lists      | 0.829
short_set_union_rest | few_large_lists      | 0.849

请务必在您自己的机器上运行 timeit 基准测试,因为结果可能会有所不同。


from __future__ import print_function
import random
import timeit
from itertools import chain
import numpy as np

def unique_concatenate(x):
    return np.unique(np.concatenate(x))

def short_set_union_rest(x):
    # This assumes x[0] is the shortest list in x
    return list(set(x[0]).union(*x[1:]))

def long_set_union_rest(x):
    # This assumes x[-1] is the longest list in x
    return list(set(x[-1]).union(*x[1:]))

def empty_set_union(x):
    return list(set().union(*x))

def set_chain(x):
    return list(set(chain(*x)))


big_range = list(range(10**7))
small_range = list(range(10**5))
many_uniques = [[random.choice(big_range) for i in range(j)] 
                for j in range(10, 10000, 10)]
many_duplicates = [[random.choice(small_range) for i in range(j)] 
              for j in range(10, 10000, 10)]
many_small_lists = [[random.choice(big_range) for i in range(10)] 
                    for j in range(10, 10000, 10)]
few_large_lists = [[random.choice(big_range) for i in range(1000)] 
                    for j in range(10, 100, 10)]

if __name__=='__main__':
    for x, n in [('many_uniques', 1), ('many_duplicates', 4), 
                 ('many_small_lists', 800), ('few_large_lists', 800)]:
        timing = dict()
        for func in [
                'unique_concatenate', 'short_set_union_rest', 'long_set_union_rest',
                'empty_set_union', 'set_chain']:
            timing[func, x] = timeit.timeit(
                '{}({})'.format(func, x), number=n,
                setup='from __main__ import {}, {}'.format(func, x))
        print('{:20} | {:20} | {}'.format('func', 'x', 'time'))
        for key, t in sorted(timing.items(), key=lambda item: item[1]):
            func, x = key
            print('{:20} | {:20} | {:.3f}'.format(func, x, t))
        print(end='\n')

关于python - 获得列表联合的最快方法 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35866067/

相关文章:

python - 获取与numpy中另一个数组的元素相对应的数组元素

python - 使用 OpenStack Python API 获取主机的 CPU、内存和硬盘信息

python - Boost.python 中的 error_already_set 做了什么以及如何在 Python C API 中类似地处理异常

python - 使字典成为单个逗号分隔字符串的最快方法

python - 如何根据条件连接 pandas 列中的字符串

python - 在 numpy 数组上“绘制”随机菱形(菱形)(测试哈里斯角检测)

python - 使用字典从数据框中选择多列

python - 在python中对列表中的文件进行排序

jQuery 列表图标更改

python - 计算时间间隔内列值的平均值