[英]how do I combine N dictionaries in list of dictionaries based on matching key:value pair?
我想實現以下目標。 它本質上是 N 個字典的組合或合並,從重復的id
中累積所有數據,並將來自多個數據源中的所有字典的所有values(except id, updated_date)
附加到最終結果中。
class A:
def __init__(self):
pass
def run(self):
return {"data":[{"id":"ID-2002-0201","updated_at":"2018-05-14T22:25:51Z","html_url":["https://github.com/ID-2002-0201"],"source":"github"},{"id":"ID-2002-0200","updated_at":"2018-05-14T21:49:15Z","html_url":["https://github.com/ID-2002-0200"],"source":"github"},{"id":"ID-2002-0348","updated_at":"2018-05-11T14:13:28Z","html_url":["https://github.com/ID-2002-0348"],"source":"github"}]}
class B:
def __init__(self):
pass
def run(self):
return {"data":[{"id":"ID-2002-0201","updated_at":"2006-03-28","html_url":["http://sample.com/files/1622"],"source":"sample"},{"id":"ID-2002-0200","updated_at":"2006-06-05","html_url":["http://sample.com/files/1880"],"source":"sample"},{"id":"ID-2002-0348","updated_at":"2007-03-09","html_url":["http://sample.com/files/3441"],"source":"sample"}]}
results = {}
data_sources = [A(),B()]
for data in data_sources:
data_stream = data.run()
for data in data_stream.get('data'):
for key, value in data.items():
if key in ['html_url']:
results.setdefault(key, []).extend(value)
elif key in ['source']:
results.setdefault(key, []).append(value)
else:
results[key] = value
print(results)
所需 output
[
{
"id":"ID-2002-0201",
"updated_at":"2018-05-14T22:25:51Z",
"html_url":[
"https://github.com/ID-2002-0201",
"https://github.com/ID-2002-0202",
"https://github.com/ID-2002-0203",
"https://github.com/ID-2002-0204"
],
"source": [
"github",
"xxx",
"22aas"
]
},
]
我有點困惑,因為您提供的所需 output 與您在代碼中提供的示例類不匹配。 但是,我想我得到了你想要的,如果我錯誤地解釋了你的問題,請糾正我。
我使用您的結果數組,就像字典一樣。 外部字典包含所有唯一 ID 作為鍵,內部字典包含 output 中所需的數據。 在循環計算之后,我只返回list(results.values())
以獲取 N 個組合的字典列表。
這是代碼:
class A:
def __init__(self):
pass
def run(self):
return {"data":[{"id":"ID-2002-0201","updated_at":"2018-05-14T22:25:51Z","html_url":["https://github.com/ID-2002-0201"],"source":"github"},{"id":"ID-2002-0200","updated_at":"2018-05-14T21:49:15Z","html_url":["https://github.com/ID-2002-0200"],"source":"github"},{"id":"ID-2002-0348","updated_at":"2018-05-11T14:13:28Z","html_url":["https://github.com/ID-2002-0348"],"source":"github"}]}
class B:
def __init__(self):
pass
def run(self):
return {"data":[{"id":"ID-2002-0201","updated_at":"2006-03-28","html_url":["http://sample.com/files/1622"],"source":"sample"},{"id":"ID-2002-0200","updated_at":"2006-06-05","html_url":["http://sample.com/files/1880"],"source":"sample"},{"id":"ID-2002-0348","updated_at":"2007-03-09","html_url":["http://sample.com/files/3441"],"source":"sample"}]}
results = {}
data_sources = [A(),B()]
for data in data_sources:
data_stream = data.run()
for data in data_stream.get('data'):
curr_id = data["id"]
result = results.setdefault(curr_id, {})
for key, value in data.items():
if key in ['html_url']:
result.setdefault(key, []).extend(value)
elif key in ['source']:
result.setdefault(key, []).append(value)
else:
result[key] = value
print(list(results.values()))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.