python - 从python字典中提取id作为列表
问题描述
我正在尝试使用 python 从嵌套字典中提取 id,但是当我运行下一个代码时它会运行错误:
nested_dictionary = {
"name": "field",
"vacant": "capable",
"employees":
{
1: {"id": 1, "name": "Joe", "px": "px1"},
2: {"id": 2, "name": "Mary", "px": "px2"},
3: {"id": 3, "name": "George", "px": "px3"},
4: {"id": 4, "name": "Louise", "px": "px4"},
5: {"id": 5, "name": "Malcolm", "px": "px5"},
6: {"id": 6, "name": "Reese", "px": "px6"},
},
"columns":
[
"col1",
"col2",
"col3",
"col4"
],
"columns_2":
[
"col5",
"col6",
"col7"
]
}
for p in nested_dictionary['employees']
print('id:' + p['id'])
控制台输出:
Type Error: i object is not subscriptable
预期输出:
id: 1
id: 2
id: 3
id: 4
id: 5
id: 6
有没有其他方法可以完成这项任务?
解决方案
推荐阅读
- r - 使用 R 中多个“键”列中的值将数据帧从长格式复杂地重塑为宽格式
- php - 选择框显示 db 值不允许更新
- amazon-web-services - AWS step 函数可以执行超过 25000 次吗?
- c# - 获取第一个表插入值到第二个表
- python - 如何修复“ValueError:发现样本数量不一致的输入变量:[10000, 60000]”?
- python - mac os上的python程序中的路径有问题吗?
- android - 如何以不同的时间间隔同步android Handlers?
- javascript - JS:将函数存储为变量
- java - 如何使用 EntityManager 通过 id 查找实体而不在一个查询中命中缓存?
- node.js - 在没有 index.html 的情况下检索用户的地理位置