我有2个非常大的数组.
这段代码运行得很慢吗?
results1 = [1,2,3..]
results2 = [1,2,3,4 ... ]
for result1 in results1:
if result1 not in results2:
print result1
使用set:
hashed = set(results2)
....
if result1 not in hashed:
Run Code Online (Sandbox Code Playgroud)
请注意,如果您的阵列非常庞大,则需要大量内存.
或者,对两个数组进行排序并使用两个索引.如果两个元素相同,则递增两个索引.如果它们不相等,则递增包含较小元素的数组的索引.