gpt4 book ai didi

python - 使用输入列表中的字典元素附加到元组列表

转载 作者:行者123 更新时间:2023-11-28 22:19:15 27 4
gpt4 key购买 nike

我有两个这样的列表:

my_list = [['can you change departure date to 30th March', '207443006734608218498'], ['can you downgrade to economy class?', '276566920664343421717'], ['book flight from San Francisco to Los Angeles on April 17-24', '897058868855606085615']]

ids_list = ['207443006734608218498', '276566920664343421717', '897058868855606085615']

我检查 my_list 中匹配的元素的 ids_list 然后我尝试追加到其他列表

我试着得到像那个最终列表:

[
('can you change departure date to 30th March',
{
'ids': {
'207443006734608218498': 'True',
'276566920664343421717': 'False',
'897058868855606085615': 'False'
}
}),
('can you downgrade to economy class?',
{
'ids': {
'207443006734608218498': 'False',
'276566920664343421717': 'True',
'897058868855606085615': 'False'
}
}),
('book flight from San Francisco to Los Angeles on April 17-24',
{
'ids': {
'207443006734608218498': 'False',
'276566920664343421717': 'False',
'897058868855606085615': 'True'
}
})
]

为此我编写了简单的循环并像那样更新字典的元组

>>> data = {} 
>>> for x in my_list:
... for y in ids_list:
... data.update({y: "True"} if x[1]==y else {y: "False"})
... print((x[0],{"ids": data}))
...

('can you change departure date to 30th March', {'ids': {'207443006734608218498': 'True', '276566920664343421717': 'False', '897058868855606085615': 'False'}})
('can you downgrade to economy class?', {'ids': {'207443006734608218498': 'False', '276566920664343421717': 'True', '897058868855606085615': 'False'}})
('book flight from San Francisco to Los Angeles on April 17-24', {'ids': {'207443006734608218498': 'False', '276566920664343421717': 'False', '897058868855606085615': 'True'}})

但是当我在列表中追加时,数据字典附加了错误的值,当我打印时,数据字典的值是正确的我应该做错什么?

>>> t = []
>>> for x in my_list:
... for y in ids_list:
... data.update({y: "True"} if x[1]==y else {y: "False"})
... t.append((x[0],{"ids":data}))
...
>>> t
[
('can you change departure date to 30th March', {'ids': {'207443006734608218498': 'False', '276566920664343421717': 'False', '897058868855606085615': 'True'}}),
('can you downgrade to economy class?', {'ids': {'207443006734608218498': 'False', '276566920664343421717': 'False', '897058868855606085615': 'True'}}),
('book flight from San Francisco to Los Angeles on April 17-24', {'ids': {'207443006734608218498': 'False', '276566920664343421717': 'False', '897058868855606085615': 'True'}})]

最佳答案

问题是这一行:

data.update({y: "True"} if x[1]==y  else {y: "False"})

它就地更新数据,而不是将名称重新绑定(bind)到新的字典对象。为了更好地理解,让我们看看通过“展开”你的循环真正放入 t 的内容:

t = [(my_list[0][0], {'ids': data}), (my_list[1][0], {'ids': data}), (my_list[2][0], {'ids': data})]

请注意,虽然外部字典(带有 'ids')每次都是不同的对象,但内部字典始终是对同一 data 对象的引用。您对 data 所做的任何更改都将在列表中的每个元素中可见。

解决方案当然是在循环的每次迭代中创建一个新对象。我能想到的最简单的方法是将您的内部循环变成字典理解:

t = []
for x in my_list:
data = {id: "True" if x[1] == id else "False" for id in ids}
t.append((x[0], {"ids": data}))

您可以进行的另一项改进是解包 x 以使代码更易读:

t = []
for request, id in my_list:
data = {x: "True" if id == x else "False" for x in ids}
t.append((request, {"ids": data}))

最后,如果您愿意,您的整个代码足够简单,可以坚持使用单行列表推导式:

t = [(request, {"ids": {x: "True" if id == x  else "False" for x in ids}}) for request, id in my_list]

关于python - 使用输入列表中的字典元素附加到元组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49897986/

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