Skip to main content
 首页 » 编程设计

python之添加到字典列表之python

2024年11月01日8jyk

我正在尝试将新的字典添加到现有的字典列表中,该列表采用新的键和值列表。

我的字典列表称为“选项”,它看起来像这样:

[{'powerpoint_color': 'blue', 'client_name': 'Sport Parents (Regrouped)'},  
{'crossbreak': 'profile_julesage', 'chart_layout': '8', 'chart_type': 'pie', 'sort_order': 'desending'}]   

我想向选项中添加一个新的字典,但我不确定该怎么做?

我想用一个名为“fixed_properties”的键来调用我的新字典,它将采用一个名为“fixed_elements”的字符串列表。

我试过这样的:

fixed_elements = [] 
 
options['fixed_properties'] = fixed_elements 

但是我得到了这个错误:

'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 是您的选项列表:

options.append({'fixed_properties': fixed_elements})