gpt4 book ai didi

python - 在 Python 中组合两个字典列表中的值时如何避免嵌套 for 循环?

转载 作者:行者123 更新时间:2023-12-04 00:55:21 25 4
gpt4 key购买 nike

假设我有两个列表:

list1 = [{"sport": 'Soccer',
"leagues": [{"id": 1001,
"name": "League1",
"events": [{"id": 100,
"home": "team1",
"away": "team2"},
{"id": 101,
"home": "team3",
"away": "team4"}]}]},
{"sport": 'Basketball',
"leagues": [{"id": 1002,
"name": "League2",
"events": [{"id": 200,
"home": "team5",
"away": "team6"},
{"id": 201,
"home": "team7",
"away": "team8"}]},
{"id": 1003,
"name": "League3",
"events": [{"id": 300,
"home": "team9",
"away": "team10"},
{"id": 301,
"home": "team11",
"away": "team12"}]}],
}
]

list2 = [{"sport": 'Soccer',
"leagues": [{"id": 1001,
"events": [{"id": 100,
"odds": {"home": 1.862, "away": 1.847}},
{"id": 101,
"odds": {"home": 1.70, "away": 2.10}}]}]},
{"sport": 'Basketball',
"leagues": [{"id": 1002,
"events": [{"id": 200,
"odds": {"home": 1.952, "away": 1.952}},
{"id": 201,
"odds": {"home": 1.90, "away": 2.05}}]},
{"id": 1003,
"events": [{"id": 300,
"odds": {"home": 1.5, "away": 2.7}},
{"id": 301,
"odds": {"home": 1.75, "away": 2.09}}]}]}]

我想将列表 1 中的事件与列表 2 中它们各自的“赔率”结合起来。实际上,这两个列表中的元素更多,为清楚起见,简化了示例。我当前的(丑陋的)解决方案:

for sport_list1 in list1:
for sport_list2 in list2:
if sport_list1['sport'] == sport_list2['sport']:
for league_list1 in sport_list1['leagues']:
for league_list2 in sport_list2['leagues']:
if league_list1['id'] == league_list2['id']:
for event_list1 in league_list1['events']:
for event_list2 in league_list2['events']:
if event_list1['id'] == event_list2['id']:
print(sport_list1['sport'], league_list1['name'], event_list1['home'], event_list1['away'], event_list2['odds'])
break

期望的输出:

Soccer League1 team1 team2 {'home': 1.862, 'away': 1.847}
Soccer League1 team3 team4 {'home': 1.7, 'away': 2.1}
Basketball League2 team5 team6 {'home': 1.952, 'away': 1.952}
Basketball League2 team7 team8 {'home': 1.9, 'away': 2.05}
Basketball League3 team9 team10 {'home': 1.5, 'away': 2.7}
Basketball League3 team11 team12 {'home': 1.75, 'away': 2.09}

有什么方法可以使它更清洁和/或更高效?

最佳答案

@Chronial 的回答很完美,但这里有另一种您可能会感兴趣的方法:

def convert_to_dicts(x):
if type(x) == list:
id_field = {"sport", "id"}.intersection(set(x[0].keys())).pop()
return {y.pop(id_field): convert_to_dicts(y) for y in x}
elif type(x) == dict:
return{z: convert_to_dicts(y) for z, y in x.items()}
return x

def recursive_dict_merge(x, y):
new_dict = {}
for key in set(x.keys()).union(set(y.keys())):
x_val = x.get(key, None)
y_val = y.get(key, None)
if type(x_val) == dict and type(y_val) == dict:
new_dict[key] = recursive_dict_merge(x_val, y_val)
else:
new_dict[key] = x_val or y_val
return new_dict


result = recursive_dict_merge(convert_to_dicts(list1), convert_to_dicts(list2))

我首先将“字典列表中的字典列表”转换为嵌套字典。

然后我使用递归来合并那些字典。

我认为这种方法更好,因为你有一个“易于使用”的 result 字典,这样可以更容易地做其他事情,例如您想要的确切 print:

for sport, leagues in result.items():
for league in leagues["leagues"].values():
for event in league["events"].values():
print(sport, league['name'], event['home'], event['away'], event['odds'])

一般来说,对于此类问题,我发现最好的第一步通常是将输入 reshape 为更易于管理的内容。嵌套字典比......等的字典列表更容易思考。

关于python - 在 Python 中组合两个字典列表中的值时如何避免嵌套 for 循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62953048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com