gpt4 book ai didi

python - 如何将列表中字典集中的每个元素组合到另一组字典列表中?

转载 作者:行者123 更新时间:2023-11-28 20:59:48 24 4
gpt4 key购买 nike

假设如果两个字典列表是

l1 = [{'name': '1st Match - Sri Lanka v India', 'place': 'R Premadasa Stadium, Colombo'}, {'name': '2nd Match - Bangladesh v India', 'place': 'R Premadasa Stadium, Colombo'}, {'name': '3rd Match - Sri Lanka v Bangladesh', 'place': 'R Premadasa Stadium, Colombo'}, {'name': '4th Match - Sri Lanka v India', 'place': 'R Premadasa Stadium, Colombo'}, {'name': '5th Match - Bangladesh v India', 'place': 'R Premadasa Stadium, Colombo'}, {'name': '6th Match - Sri Lanka v Bangladesh', 'place': 'R Premadasa Stadium, Colombo'}, {'name': 'Final - TBC v TBC', 'place': 'R Premadasa Stadium, Colombo'}]
l2 = [{'date': 'Tue Mar 6'}, {'date': 'Thu Mar 8'}, {'date': 'Sat Mar 10'}, {'date': 'Mon Mar 12'}, {'date': 'Wed Mar 14'}, {'date': 'Fri Mar 16'}, {'date': 'Sun Mar 18'}]

我想将 l2 中的元素组合到 l1 中的元素,如下所示。

 L3 = [{'name': '1st Match - Sri Lanka v India', 'place': 'R Premadasa Stadium, Colombo','date': 'Tue Mar 6'}, {'name': '2nd Match - Bangladesh v India', 'place': 'R Premadasa Stadium, Colombo','date': 'Thu Mar 8'}, {'name': '3rd Match - Sri Lanka v Bangladesh', 'place': 'R Premadasa Stadium, Colombo','date': 'Sat Mar 10'}, {'name': '4th Match - Sri Lanka v India', 'place': 'R Premadasa Stadium, Colombo','date': 'Mon Mar 12'}, {'name': '5th Match - Bangladesh v India', 'place': 'R Premadasa Stadium, Colombo','date': 'Wed Mar 14'}, {'name': '6th Match - Sri Lanka v Bangladesh', 'place': 'R Premadasa Stadium, Colombo','date': 'Fri Mar 16'}, {'name': 'Final - TBC v TBC', 'place': 'R Premadasa Stadium, Colombo','date': 'Sun Mar 18'}]

我可以使用哪个函数来执行此操作?

最佳答案

你可以这样做:

l1 = [{'name': '1st Match - Sri Lanka v India', 'place': 'R Premadasa Stadium, Colombo'}, {'name': '2nd Match - Bangladesh v India', 'place': 'R Premadasa Stadium, Colombo'}, {'name': '3rd Match - Sri Lanka v Bangladesh', 'place': 'R Premadasa Stadium, Colombo'}, {'name': '4th Match - Sri Lanka v India', 'place': 'R Premadasa Stadium, Colombo'}, {'name': '5th Match - Bangladesh v India', 'place': 'R Premadasa Stadium, Colombo'}, {'name': '6th Match - Sri Lanka v Bangladesh', 'place': 'R Premadasa Stadium, Colombo'}, {'name': 'Final - TBC v TBC', 'place': 'R Premadasa Stadium, Colombo'}]
l2 = [{'date': 'Tue Mar 6'}, {'date': 'Thu Mar 8'}, {'date': 'Sat Mar 10'}, {'date': 'Mon Mar 12'}, {'date': 'Wed Mar 14'}, {'date': 'Fri Mar 16'}, {'date': 'Sun Mar 18'}]

res = [{**d[0],**d[1]} for d in zip(l1,l2)]
print(res)

输出:

[{'name': '1st Match - Sri Lanka v India', 'place': 'R Premadasa Stadium, Colombo', 'date': 'Tue Mar 6'}, {'name': '2nd Match - Bangladesh v India', 'place': 'R Premadasa Stadium, Colombo', 'date': 'Thu Mar 8'}, {'name': '3rd Match - Sri Lanka v Bangladesh', 'place': 'R Premadasa Stadium, Colombo', 'date': 'Sat Mar 10'}, {'name': '4th Match - Sri Lanka v India', 'place': 'R Premadasa Stadium, Colombo', 'date': 'Mon Mar 12'}, {'name': '5th Match - Bangladesh v India', 'place': 'R Premadasa Stadium, Colombo', 'date': 'Wed Mar 14'}, {'name': '6th Match - Sri Lanka v Bangladesh', 'place': 'R Premadasa Stadium, Colombo', 'date': 'Fri Mar 16'}, {'name': 'Final - TBC v TBC', 'place': 'R Premadasa Stadium, Colombo', 'date': 'Sun Mar 18'}]

可以看看here查看如何合并词典。

关于python - 如何将列表中字典集中的每个元素组合到另一组字典列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49151289/

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