性能,優美和可讀性是要求我有字典的數組「的最佳途徑」將對象的兩個元素添加到列表而不重複它們的最佳方式是什麼?
:
items = [
{'id1' : 1, 'id2' : 2, 'other' : 'xxx'},
{'id1' : 1, 'id2' : 3, 'other' : 'yyy'},
{'id1' : 2, 'id2' : 4, 'other' : 'zzz'}
]
結果應該是:ids = [1,2,3,4]
(ID1和ID2的列表)
編輯: 事情是這樣的:
ids = []
for item in items:
if item.id1 not in ids:
ids.append(item.id1)
if item.id2 not in ids:
ids.append(item.id2)
請注意,這些值實際上沒有排序。除非你明確要'id1'和'id2'。你需要原始訂單嗎? – jamylak 2013-03-22 13:13:51