基本上有 15 个问题从Question 1
到Question 15
作为父词典中的嵌套词典Questions
。
但现在我想用这个列表更新这本词典,其中也有 15 个问题:
new_list = ['Question 6', 'Question 7', 'Question 8', 'Question 9', 'Question 10', 'Question 11', 'Question 12', 'Question 13', 'Question 14', 'Question 15', 'Question 16', 'Question 17', 'Question 18', 'Question 19', 'Question 20']
这只是Question n + 5
人类语言。
但我一整天都在挣扎。我观察到的奇怪行为是:
方法1-
for ind, val in enumerate(list(q_dict['Questions'].keys())): q_dict['Questions'][new_list[ind]] = q_dict['Questions'].pop(val)
结果是:
{'Paper Name': 'Paper 8.jpg', 'Category': 1, 'Questions': {'Question 16': {'Question Type': 1, 'Question Point': [], 'Expected Answers': [], 'Student Answers': []}, 'Question 17': {'Question Type': 3, 'Question Point': [], 'Expected Answers': [], 'Student Answers': []}, 'Question 18': {'Question Type': 3, 'Question Point': [], 'Expected Answers': [], 'Student Answers': []}, 'Question 19': {'Question Type': 3, 'Question Point': [], 'Expected Answers': [], 'Student Answers': []}, 'Question 20': {'Question Type': 3, 'Question Point': [], 'Expected Answers': [], 'Student Answers': []}}}
方法2-
for ind, val in enumerate(list(q_dict['Questions'].keys())): q_dict['Questions'][new_list[ind]] = q_dict['Questions'][val] del q_dict['Questions'][val]
结果:
{'Paper Name': 'Paper 8.jpg', 'Category': 1, 'Questions': {'Question 6': {'Question Type': 1, 'Question Point': [], 'Expected Answers': [], 'Student Answers': []}, 'Question 7': {'Question Type': 3, 'Question Point': [], 'Expected Answers': [], 'Student Answers': []}, 'Question 8': {'Question Type': 3, 'Question Point': [], 'Expected Answers': [], 'Student Answers': []}, 'Question 9': {'Question Type': 3, 'Question Point': [], 'Expected Answers': [], 'Student Answers': []}, 'Question 10': {'Question Type': 3, 'Question Point': [], 'Expected Answers': [], 'Student Answers': []}}}
慕的地8271018
一只名叫tom的猫
相关分类