有一个 C++ 比较可以从列表列表中获取列表的并集:找到集合并集的最快方法 https://stackoverflow.com/questions/11362002/the-fastest-way-to-find-union-of-sets
还有其他几个与 python 相关的问题,但没有提出联合列表的最快方法:
- 在 Python 中查找列表列表的并集 https://stackoverflow.com/questions/26445907/finding-a-union-of-lists-of-lists-in-python
- 在 Python 中展平浅列表 https://stackoverflow.com/questions/406121/flattening-a-shallow-list-in-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.
[out]:
1.39586925507e-05
1.09834671021e-05
取出铸造集的变量列出:
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.
[out]:
1.22241973877e-05
1.02684497833e-05
这是我尝试打印中间计时(没有列表转换)时的完整输出:http://pastebin.com/raw/y3i6dXZ8 http://pastebin.com/raw/y3i6dXZ8
为什么会这样list(set(chain(*x)))
花费的时间少于list(set().union(*x))
?
是否有另一种方法可以实现相同的列表并集?使用numpy
or pandas
or sframe
或者其他的东西? 替代方案更快吗?