하나의 반복 가능하고 다른 반복 가능하지 않은 모든 항목을 찾으려면 몇 가지 코드를 작성했습니다. 나는 원래 세트 차이를 사용했지만, 각 세트에 수백만 개의 아이템이 저장되어 있기 때문에 계산이 다소 느렸다.Pythonic iterable difference
def differences(a_iter, b_iter):
a_items, b_items = set(), set()
def remove_or_add_if_none(a_item, b_item, a_set, b_set):
if a_item is None:
if b_item in a_set:
a_set.remove(b_item)
else:
b_set.add(b)
def remove_or_add(a_item, b_item, a_set, b_set):
if a in b_set:
b_set.remove(a)
if b in a_set:
a_set.remove(b)
else:
b_set.add(b)
return True
return False
for a, b in itertools.izip_longest(a_iter, b_iter):
if a is None or b is None:
remove_or_add_if_none(a, b, a_items, b_items)
remove_or_add_if_none(b, a, b_items, a_items)
continue
if a != b:
if remove_or_add(a, b, a_items, b_items) or \
remove_or_add(b, a, b_items, a_items):
continue
a_items.add(a)
b_items.add(b)
return a_items, b_items
그러나, 위의 코드 그래서 난 개선을위한 대안이나 제안을 찾고 있어요 매우 파이썬하지 않는 것 : 나는 천 대부분의 몇 가지 차이점에있을 것입니다 알고 있기 때문에 나는 아래 버전을 썼다.
내장 된 세트의 차이보다 얼마나 더 빠릅니까? –