[英]Updating dictionary values using keys and values from lists
我希望能夠索引字典並通過使用特定列表中的鍵並將值寫入該列表中的這些鍵來替換特定鍵的值。
代碼
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)
追溯
所需 output
{'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}
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)})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.