gpt4 book ai didi

python - 在列表列表python中添加键和值

转载 作者:行者123 更新时间:2023-12-05 03:17:09 25 4
gpt4 key购买 nike

我的数据看起来像这样 [[{'title': 'Line'}], [{'title': 'asd'}]]。我想为列表中的每个列表添加一个新的键和值。

我试过了,但出现错误“列表”对象不是映射。有什么建议吗?

data = [[{'title': 'Line'}], [{'title': 'asd'}]]
titleID = [{'id': 373}, {'id': 374}]
combine = [{**dict_1, **dict_2}
for dict_1, dict_2 in zip(char_id, data )]

我想要的输出是这样的:

[[{'id': 373, 'title': 'Line'}], [{'id': 374, 'title': 'asd'}]]

最佳答案

试试这个列表理解和解包

data = [[{'title': 'Line'}], [{'title': 'asd'}]]
titleID = [{'id': 373}, {'id': 374}]
[[{**i[0], **j}] for i,j in zip(data, titleID)]

输出

[[{'title': 'Line', 'id': 373}], [{'title': 'asd', 'id': 374}]]

关于python - 在列表列表python中添加键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74260188/

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