我正在使用Twitch API,并且得到了以下字典:
用户数据
{"data": [
{"display_name": "John", "id": "123"},
{"display_name": "Frank", "id": "456"},
{"display_name": "Billy", "id": "789"}]}
流数据
{"data": [
{"id": "333444", "user_id": "456", "title": "Franks Stream"}
{"id": "555666", "user_id": "789", "title": "Billys Stream"}
{"id": "111222", "user_id": "123", "title": "Johns Stream"}]}
我不能保证两者的顺序都相同,所以我想使用id / user_id将两者结合:
组合数据
{"data": [
{"id": "333444", "user_id": "456", "title": "Franks Stream", "display_name": "Frank"}
{"id": "555666", "user_id": "789", "title": "Billys Stream", "display_name": "Billy"}
{"id": "111222", "user_id": "123", "title": "Johns Stream", "display_name": "John"}]}
如果可能,我想合并更多字段,但是是否可以像这样加入这些词典?
答案 0 :(得分:1)
设置
user_data = {
"data": [
{"display_name": "John", "id": "123"},
{"display_name": "Frank", "id": "456"},
{"display_name": "Billy", "id": "789"}]}
stream_data = {
"data": [
{"id": "333444", "user_id": "456", "title": "Franks Stream"},
{"id": "555666", "user_id": "789", "title": "Billys Stream"},
{"id": "111222", "user_id": "123", "title": "Johns Stream"}]}
解决方案
使用字典理解,您可以在id
字段上键入新的用户数据字典,然后使用该数据更新流数据。
new_user_data = {row.get("id"): row.get('display_name')
for row in user_data['data']}
>>> new_user_data
{'123': 'John', '456': 'Frank', '789': 'Billy'}
现在使用此词典更新数据中的display_name
(如果您不想变异原始数据,则可能希望创建一个副本)。
for row in stream_data['data']:
id_ = row.get('user_id')
row['display_name'] = new_user_data[id_]
>>> stream_data['data']
[{'display_name': 'Frank',
'id': '333444',
'title': 'Franks Stream',
'user_id': '456'},
{'display_name': 'Billy',
'id': '555666',
'title': 'Billys Stream',
'user_id': '789'},
{'display_name': 'John',
'id': '111222',
'title': 'Johns Stream',
'user_id': '123'}]
如果要存储多个用户数据作为值(即,不只是id
作为键,而display_name
作为值),则可以使用{{3} }来固定它们。
user_data = {
"data": [
{"display_name": "John", "id": "123", "description": "boring"},
{"display_name": "Frank", "id": "456", "description": "smart"},
{"display_name": "Billy", "id": "789", "description": "funny"}]}
from collections import namedtuple
UserData = namedtuple('UserData', ('display_name', 'description'))
for row in user_data['data']:
id_ = row.pop('id')
new_user_data[id_] = UserData(**row)
for row in stream_data['data']:
id_ = row.get('user_id')
row.update(**new_user_data[id_].__dict__)
>>> new_user_data
{'123': UserData(display_name='John', description='boring'),
'456': UserData(display_name='Frank', description='smart'),
'789': UserData(display_name='Billy', description='funny')}
>>> stream_data
{'data': [
{'description': 'smart',
'display_name': 'Frank',
'id': '333444',
'title': 'Franks Stream',
'user_id': '456'},
{'description': 'funny',
'display_name': 'Billy',
'id': '555666',
'title': 'Billys Stream',
'user_id': '789'},
{'description': 'boring',
'display_name': 'John',
'id': '111222',
'title': 'Johns Stream',
'user_id': '123'}]}
答案 1 :(得分:0)
您可以使用itertools.groupby
:
import itertools
from functools import reduce
d = {'data': [{'display_name': 'John', 'id': '123'}, {'display_name': 'Frank', 'id': '456'}, {'display_name': 'Billy', 'id': '789'}]}
d1 = {'data': [{'id': '333444', 'user_id': '456', 'title': 'Franks Stream'}, {'id': '555666', 'user_id': '789', 'title': 'Billys Stream'}, {'id': '111222', 'user_id': '123', 'title': 'Johns Stream'}]}
combined = sorted(d['data']+d1['data'], key=lambda x:x.get('user_id', x['id']))
new_data = [list(b) for _, b in itertools.groupby(combined, key=lambda x:x.get('user_id', x['id']))]
final_results = {'data':[reduce(lambda x, y:{**x, **y}, i) for i in new_data]}
输出:
{'data': [
{'display_name': 'John', 'id': '111222', 'user_id': '123', 'title': 'Johns Stream'},
{'display_name': 'Frank', 'id': '333444', 'user_id': '456', 'title': 'Franks Stream'},
{'display_name': 'Billy', 'id': '555666', 'user_id': '789', 'title': 'Billys Stream'}
]}
答案 2 :(得分:0)
让我们在这里使用pandas
import pandas as pd
df2=pd.DataFrame(d2['data'])
df1=pd.DataFrame(d1['data']).rename(columns={'id':'user_id'})
{'data':df1.merge(df2).to_dict('r')}
Out[150]:
{'data': [{'display_name': 'John',
'id': '111222',
'title': 'Johns Stream',
'user_id': '123'},
{'display_name': 'Frank',
'id': '333444',
'title': 'Franks Stream',
'user_id': '456'},
{'display_name': 'Billy',
'id': '555666',
'title': 'Billys Stream',
'user_id': '789'}]}
答案 3 :(得分:0)
您可以先将用户数据字典转换为从id
到display_name
的映射:
u = {"data": [
{"display_name": "John", "id": "123"},
{"display_name": "Frank", "id": "456"},
{"display_name": "Billy", "id": "789"}]}
s = {"data": [
{"id": "333444", "user_id": "456", "title": "Franks Stream"},
{"id": "555666", "user_id": "789", "title": "Billys Stream"},
{"id": "111222", "user_id": "123", "title": "Johns Stream"}]}
m = dict(reversed(tuple(i.values())) for i in u['data'])
combined = {'data': [{**i, 'display_name': m[i['user_id']]} for i in s['data']]}
combined
变为:
{'data': [{'id': '333444', 'user_id': '456', 'title': 'Franks Stream', 'display_name': 'Frank'}, {'id': '555666', 'user_id': '789', 'title': 'Billys Stream', 'display_name': 'Billy'}, {'id': '111222', 'user_id': '123', 'title': 'Johns Stream', 'display_name': 'John'}]}