2016-11-25 105 views
-2

我的词典列表:更新列表

my_list = [{'question_text': 'question text'}, {'question_text': 'question text'}] 

我需要更新my_list所以它看起来像:

my_list = [{'question_text': 'question text', 'answer_text': 'answer text'}, 
      {'question_text': 'question text', 'answer_text': 'answer text'}] 

我需要做my_list在for循环中更新,因为answer_text的数据来自其他列表。

编辑: 我可以改变我初始化my_list的方式,通过将一个空的刺痛作为价值answer_text

my_list = [{'question_text': 'question text', 'answer_text': ''}, 
      {'question_text': 'question text', 'answer_text': ''}] 
+1

这样的清单应最初由“问题”/“答案”对组成,以设置正确的关系 – RomanPerekhrest

+3

到目前为止,这么好。你有什么问题? – Holloway

+2

你在问怎么把东西附加到列表中? – doctorlove

回答

0
for _dict in my_list: 
    _dict['answer_text'] = 'answer text' 

print my_list 

结果是:

[{'question_text': 'question text', 'answer_text': 'answer text'},{'question_text': 'question text', 'answer_text': 'answer text'}]