的字典替换值我有一本字典,像这样:在列表/元组蟒蛇
d = {}
d['key1'] = [('tuple1a', 'tuple1b', ['af1', 'af2', 'af3']),
('tuple2a', 'tuple2b', ['af4', 'af5', 'af6']),
('tuple3a', 'tuple3b', ['af7', 'af8', 'af9'])]
我想编写一个函数,让我更新值(例如['af1','af2','af3']
)的列表部分。下面的代码的工作原理是不同的值进行过滤,以获得正确的列表值的范围内:
def update_dict(dictionary, key, tuple_a, tuple_b, new_list=None):
for k,v in dictionary.items():
if key in k:
for i in v:
if tuple_a in i:
if tuple_b in i:
#di.update(i[2], new_lst) #this is what I'd like to do but can't get the right syntax
return dictionary
我想添加类似di.update(i[2], new_lst)
我的问题是如何以新的清单仅更新列表值?
这是一个元组字典。你不能更新元组。但我想知道你可以单独更改引用列表。 – Dandekar
因为元组是不可变的,所以我可以像这样重新创建字典:'d.update({k:[(tuple_a,tuple_b,aod_nt)]})'但它只创建一个关键字:值对的字典。我怎样才能保存字典中的其他值? – e9e9s