我想對包含相同“id”值的字典進行重復資料洗掉。
字典串列:
example = [{'term': 'potato', 'id': 10}, {'term': 'potatoes', 'id': 10}, {'term': 'apple', 'id': 7}]
期望的輸出:
example = [{'term': 'potato', 'id': 10}, {'term': 'apple', 'id': 7}]
目前我只能洗掉所有重復項而不是保留一個;或者只洗掉那些完全相同的字典,而我只想洗掉那些具有相同 id 值的字典。
示例代碼(嘗試):
import ast
new_list = []
seen_keys = set()
for term in example:
d = ast.literal_eval(term) #had to convert a string-dict to a dict first because the dictionaries were transformed to a string in a Solr database
if d['id'] not in seen_keys:
new_list.append(d)
seen_keys.add(d['id'])
uj5u.com熱心網友回復:
無需使用ast.literal_eval:
example = [{'term': 'potato', 'id': 10}, {'term': 'potatoes', 'id': 10}, {'term': 'apple', 'id': 7}]
seen_keys = set()
new_list = []
for d in example:
if d["id"] not in seen_keys:
seen_keys.add(d["id"])
new_list.append(d)
print(new_list)
輸出
[{'term': 'potato', 'id': 10}, {'term': 'apple', 'id': 7}]
如果您對O(n)單線感興趣,請使用:
new_list = list({ d["id"] : d for d in example[::-1]}.values())[::-1]
print(new_list)
輸出 (來自單行)
[{'term': 'potato', 'id': 10}, {'term': 'apple', 'id': 7}]
uj5u.com熱心網友回復:
或者使用單行串列理解enumerate:
>>> [d for i, d in enumerate(example) if d['id'] not in [x['id'] for x in example[i 1:]]]
[{'term': 'potatoes', 'id': 10}, {'term': 'apple', 'id': 7}]
>>>
uj5u.com熱心網友回復:
你可以試試這個
example = [
{"term": "potato", "id": 10},
{"term": "potatoes", "id": 10},
{"term": "apple", "id": 7},
]
ids = set()
for item in example:
ids.add(item["id"])
results = []
for item in example:
if item["id"] in ids:
results.append(item)
ids.remove(item["id"])
print(results)
uj5u.com熱心網友回復:
它可以很容易地完成:
test_list = [{'term': 'potato', 'id': 10}, {'term': 'potatoes', 'id': 10}, {'term': 'apple', 'id': 7}]
res = []
[res.append(x) for x in test_list if x['id'] not in [y['id'] for y in res]]
print(res)
uj5u.com熱心網友回復:
稍微編輯您的代碼后:
example = [{'term': 'potato', 'id': 10}, {'term': 'potatoes', 'id': 10}, {'term': 'apple', 'id': 7}]
new_list = []
seen_keys = set()
for i in example:
if i['id'] not in seen_keys:
new_list.append(i)
seen_keys.add(i['id'])
print(new_list)
輸出:
[{'term': 'potato', 'id': 10}, {'term': 'apple', 'id': 7}]
uj5u.com熱心網友回復:
我有點喜歡uniqueBy為這類問題制作一個通用函式:
example = [{'term': 'potato', 'id': 10}, {'term': 'potatoes', 'id': 10}, {'term': 'apple', 'id': 7}]
def uniqueBy (f):
return lambda a: { f(x): x for x in a }
uniqueById = uniqueBy(lambda x: x['id'])
print("{}".format(uniqueById(example).values()))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/311490.html
上一篇:如何修改字典中的串列值?
