python - function to add dicts to a nested dict that give increasing keys
问题描述
Lets say that I have an empty dictionary. How would I go about writing a function that would add other dictionaries to the empty one, giving it a key that would increase for every new dictionary added?
So it would result in something like:
{0: {'name': 'pork', 'cals': 100, 'pro': 10, 'sugar': 1},
1: {'name': 'chicken', 'cals': 190, 'pro': 19, 'sugar': 19},
2: {'name': 'beef', 'cals': 160, 'pro': 12, 'sugar': 2}}
解决方案
dict_of_dicts = dict(enumerate(list_of_dicts))
推荐阅读
- java - 平台 java.util.ArrayList
(没有注释)需要显式注册 JsonAdapter android - python - Google Colab 中的光栅通道
- xml - 使用 Ansible 在 xml 属性列表中添加多个值
- reactjs - 尝试在已部署的反应客户端和快速服务器上上传图像文件时出现 503 错误,然后是 CORS 错误
- python - 如何抑制 PIP 安装日志中的 URL?
- django - 不允许使用 GET 方法:django rest 框架中的 /api/banners
- haskell - 'p' 在函数类型中代表什么?
- swift - Swift,macOS:在该位置打开终端窗口
- linux - 如何使用 shell 脚本将包添加到 package.json 中?
- java - 为什么整数对象是按值传递而不是按引用传递的?