我希望能够索引一个字典,并通过在特定列表中使用键并从该列表中向这些键写入值来替换它的值。
码
dicty = {"NDS" : 1, "TCT": 2, "ET" : 3, "ACC" : 4,"Ydist" : 5, "Diam" : 6}
tem = ["NDS", "TCT"]
circ = ["ET", "ACC"]
jit = ["Ydist", "Diam"]
def cal_loop(cal_vers):
if cal_vers == temp_calibration:
print("DO TEMP CALIBRATION")
tem_results = [19,30]
dict_keys = tem
dicty[[dict_keys][0]] = tem_results[0]
print(dicty["NDS"])
temp_calibration = 6
cal_loop(temp_calibration)
print(dicty)溯源

期望输出
{'NDS': 19, 'TCT': 2, 'ET': 3, 'ACC': 4, 'Ydist': 5, 'Diam': 6}
#I also want to know how to do both keys in the list given e.g.
{'NDS': 19, 'TCT': 30, 'ET': 3, 'ACC': 4, 'Ydist': 5, 'Diam': 6}发布于 2020-12-03 11:11:40
tem = ["NDS", "TCT"]
tem_results = [19,30]
for k, v in zip(tem, tem_results):
dicty[k] = v问题在于dicty[[dict_keys][0]] = tem_results[0]。您必须循环考虑这两个列表并更新字典,或者创建一个新的词典,并使用以下方法更新现有的字典:
dicty.update({k: v for k, v in zip(tem, tem_results)})https://stackoverflow.com/questions/65124804
复制相似问题