大量のデータに対する標準的なアプローチは、これに相当します。
list_1 が「マスター」(重複なし) であり、list_2 が「更新」であり、重複がある可能性があると想定します。
iter_1 = iter( sorted(list_1) ) # Essentially SELECT...ORDER BY
iter_2 = iter( sorted(list_2) )
eof_1 = False
eof_2 = False
try:
item_1 = iter_1.next()
except StopIteration:
eof_1= True
try:
item_2 = iter_2.next()
except StopIteration:
eof_2= True
while not eof_1 and not eof_2:
if item_1 == item_2:
# do your update to create the new master list.
try:
item_2 = iter_2.next()
except StopIteration:
eof_2= True
elif item_1 < item_2:
try:
item_1 = iter_1.next()
except StopIteration:
eof_1= True
elif item_2 < item_1:
# Do your insert to create the new master list.
try:
item_2 = iter_2.next()
except StopIteration:
eof_2= True
assert eof_1 or eof_2
if eof_1:
# item_2 and the rest of list_2 are inserts.
elif eof_2:
pass
else:
raise Error("What!?!?")
はい、潜在的な並べ替えが含まれます。ファイル システムに書き戻すときに list_1 がソートされた順序で保持されている場合、かなりの時間を節約できます。list_2 をソートされた状態に保つ構造に蓄積できる場合、かなりの時間を節約できます。
冗長で申し訳ありませんが、どのイテレータが を発生させたかを知る必要があるStopIteration
ため、(自明に) while ループ全体を big-old-try ブロックでラップすることはできません。