두 개의 파이썬 사전 목록, entries9
및 entries10
이 있습니다. 항목을 비교하고 공동 항목을 joint_items
이라는 새 목록에 작성하려고합니다. 또한 일치하지 않는 항목을 unmatched_items_9
과 unmatched_items_10
의 두 가지 새 목록에 저장하려고합니다.Pythonic 방식으로 두 목록을 비교하고 비교할 수없는 항목을 인쇄합니까?
이것은 내 코드입니다. joint_items
및 unmatched_items_9
(외부 목록에 있음)을 얻는 것은 매우 쉽습니다. 그러나 내부 목록에있는 unmatched_items_10
을 얻으려면 어떻게해야합니까?
for counter, entry1 in enumerate(entries9):
match_found = False
for counter2,entry2 in enumerate(entries10):
if match_found:
continue
if entry1[a]==entry2[a] and entry1[b]==entry2[b]: # the dictionaries only have some keys in common, but we care about a and b
match_found = True
joint_item = entry1
joint_items.append(joint_item)
#entries10.remove(entry2) # Tried this originally, but realised it messes with the original list object!
if match_found:
continue
else:
unmatched_items_9.append(entry1)
성능은 실제로 일회성 스크립트이므로 문제가되지 않습니다.
내부 루프의 'if match_found : break' (끝 부분)로'if match_found : continue '를 대체 할 수있을 것 같습니다. – SiggyF
좋은 지적 @SiggyF! 예, 저는 할수 있습니다. – AP257