我有大量成对的整数对。例如
pairs = [((3, 5), (5, 5)), ((1, 1), (2, 5)), ((5, 1), (4, 3))]
我还有一大堆整数对。例如,
nums = [(2, 5), (4, 2), (5, 2)]
我想从对中删除任何在 nums 中有任何对的对。例如,
nums = set(nums)
pairs = [((x1,y1),(x2,y2)) for ((x1,y1),(x2,y2)) in pairs if not (set([(x1,y1),(x2,y2)]) & nums)]
这给
[((3, 5), (5, 5)), ((5, 1), (4, 3))]
问题是当pairs 和nums 很大时,这非常慢。你怎么能加快这个速度?
慢速输入示例:
import random
nums = [(random.randint(1,50000),random.randint(1,50000)) for i in xrange(1000000)]
pairs = [((random.randint(1,50000),random.randint(1,50000)), (random.randint(1,50000),random.randint(1,50000))) for i in xrange(8000000)]