我有两个这样的列表:
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'}})
但是当我在列表中追加时,当我打印出来时,数据dict附加了错误的值,那么数据字典值是正确的 我该怎么办?
>>> 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'}})]
答案 0 :(得分:3)
问题在于这一行:
data.update({y: "True"} if x[1]==y else {y: "False"})
它就地更新data
,而不是将名称重新绑定到新的字典对象。为了更好地理解,让我们通过“展开”循环来看看真正放入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]
答案 1 :(得分:1)
您可以通过列表理解来重构逻辑。
此外,将输入my_list
转换为字典以提高效果。
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_dict = dict(my_list)
res = [(k, {'id': {my_id: my_id == my_list_dict[k] for my_id in ids_list}}) \
for k, v in my_list_dict.items()]
<强>结果强>
[('can you change departure date to 30th March',
{'id': {'207443006734608218498': True,
'276566920664343421717': False,
'897058868855606085615': False}}),
('can you downgrade to economy class?',
{'id': {'207443006734608218498': False,
'276566920664343421717': True,
'897058868855606085615': False}}),
('book flight from San Francisco to Los Angeles on April 17-24',
{'id': {'207443006734608218498': False,
'276566920664343421717': False,
'897058868855606085615': True}})]