添加到字典列表-python
我正在尝试将一个新的dict添加到现有的字典列表中,其中包含一个新的键和值列表 我的字典列表称为“选项”,如下所示:添加到字典列表-python,python,list,dictionary,append,Python,List,Dictionary,Append,我正在尝试将一个新的dict添加到现有的字典列表中,其中包含一个新的键和值列表 我的字典列表称为“选项”,如下所示: [{'powerpoint_color': 'blue', 'client_name': 'Sport Parents (Regrouped)'}, {'crossbreak': 'profile_julesage', 'chart_layout': '8', 'chart_type': 'pie', 'sort_order': 'desending'}] 我想在选项中添
[{'powerpoint_color': 'blue', 'client_name': 'Sport Parents (Regrouped)'},
{'crossbreak': 'profile_julesage', 'chart_layout': '8', 'chart_type': 'pie', 'sort_order': 'desending'}]
我想在选项中添加一个新的dict,但我不确定如何添加
我想用一个名为“fixed_properties”的键调用我的新dict,它将包含一个名为“fixed_元素”的字符串列表
我试过这样的方法:
fixed_elements = []
options['fixed_properties'] = fixed_elements
options = {
"whatever_properties": {
'powerpoint_color': 'blue',
'client_name': 'Sport Parents (Regrouped)'
}
}
但我有一个错误:
'dict' object has no attribute 'append'
这就是我希望“选项”这句话的意思:
[{'powerpoint_color': 'blue', 'client_name': 'Sport Parents (Regrouped)'},
{'crossbreak': 'profile_julesage', 'chart_layout': '8', 'chart_type': 'pie', 'sort_order': 'desending'}
{'fixed_properties': ['q1','q4','q6']}]
有什么建议吗
谢谢。如果
选项
是您的选项列表:
options.append({'fixed_properties': fixed_elements})
首先,您的
options
变量是一个列表,因此options['fixed_properties']
不起作用,因为您只能根据列表中的对象的索引号引用它们
您最好像这样构造选项
变量:
fixed_elements = []
options['fixed_properties'] = fixed_elements
options = {
"whatever_properties": {
'powerpoint_color': 'blue',
'client_name': 'Sport Parents (Regrouped)'
}
}
例如,这允许您使用选项[“which_properties”][“powerpoint_color”]
语法获取所需的任何属性
使用此选项,您的代码
options[“fixed\u options”]=fixed\u元素将起作用。您显示的代码不会产生该错误;相反,它会给出一个关于列表options
的非整数索引的错误。你到底试了什么?(请注意,{}
是一个空的dict,如果您实际尝试的是将'q1'等附加到它,那么这显然不起作用。)我的坏的、固定的元素是一个空列表,它存储具有固定属性的问题。更新。这很有效,谢谢。我这样做了:options.append({'fixed_props':fixed_elements})