3 回答
TA贡献2037条经验 获得超6个赞
# user parameters
role = 'role'
key = 'tech_support'
# method One
result = []
for i in newlist:
for j in newlist[i]:
if newlist[i][j][role] is key:
result.append(newlist[i][j])
# method two
output = [newlist[i][j] for i in newlist for j in newlist[i] if newlist[i][j][role] is key]
TA贡献1868条经验 获得超4个赞
这真是一团糟。你应该尝试另一种方法。但如果你坚持尝试这个代码:
new_list={1:{1: {'_name': 'michael', 'id': '001', 'rating': '10', 'role': 'general_manager'},
2: {'_name': 'mohamad', 'id': '002', 'rating': '9.5', 'role': 'team_leader'},
3: {'_name': 'johnny', 'id': '003', 'rating': '9', 'role': 'developer'},
4: {'_name': 'nicholos', 'id': '004', 'rating': '9', 'role': 'tech_support'}},
2: {1: {'_name': 'michael', 'id': '001', 'rating': '10', 'role': 'general_manager'},
2: {'_name': 'mohamad', 'id': '002', 'rating': '9.5', 'role': 'team_leader'},
3: {'_name': 'johnny', 'id': '003', 'rating': '9', 'role': 'developer'},
5: {'_name': 'nawaz', 'id': '005', 'rating': '8.5', 'role': 'intern'}},
3: {1: {'_name': 'michael', 'id': '001', 'rating': '10', 'role': 'general_manager'},
2: {'_name': 'mohamad', 'id': '002', 'rating': '9.5', 'role': 'team_leader'},
4: {'_name': 'rogers', 'id': '004', 'rating': '9', 'role': 'tech_support'},
5: {'_name': 'nawaz', 'id': '005', 'rating': '8.5', 'role': 'intern'}},
4: {1: {'_name': 'michael', 'id': '001', 'rating': '10', 'role': 'general_manager'},
3: {'_name': 'johnny', 'id': '003', 'rating': '9', 'role': 'developer'},
4: {'_name': 'gargano', 'id': '004', 'rating': '9', 'role': 'tech_support'},
5: {'_name': 'nawaz', 'id': '005', 'rating': '8.5', 'role': 'intern'}},
5: {2: {'_name': 'mohamad', 'id': '002', 'rating': '9.5', 'role': 'team_leader'},
3: {'_name': 'johnny', 'id': '003', 'rating': '9', 'role': 'developer'},
4: {'_name': 'williams', 'id': '004', 'rating': '9', 'role': 'tech_support'},
5: {'_name': 'nawaz', 'id': '005', 'rating': '8.5', 'role': 'intern'}}}
for k in new_list.values():
for l in k.values():
for m in l.values():
if 'tech_support' in m:
print(l)
TA贡献1803条经验 获得超6个赞
您可以尝试下面的代码并查看是否获得所需的输出吗?
for key, val in newlist.items():
for k1, v1 in val.items():
if v1['role'] == 'tech_support':
print(v1)
添加回答
举报