python:如何访问字典中也在另一个字典中的元素

示例字典


newlist={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 range(len(new_list)):

    for l in k:

        m = new_list[k][role]['tech_support']

我想从列表中获取所有“tech_support”角色。我如何访问字典中的键值,而我也在另一个字典中访问键值


jeck猫
浏览 109回答 3
3回答

阿晨1998

# user parameters role = 'role'key = 'tech_support'# method Oneresult = []for i in newlist:    for j in newlist[i]:        if newlist[i][j][role] is key:            result.append(newlist[i][j])# method twooutput = [newlist[i][j] for i in newlist for j in newlist[i] if newlist[i][j][role] is  key]

MYYA

这真是一团糟。你应该尝试另一种方法。但如果你坚持尝试这个代码: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)

慕码人8056858

您可以尝试下面的代码并查看是否获得所需的输出吗?for key, val in newlist.items():    for k1, v1 in val.items():        if v1['role'] == 'tech_support':            print(v1)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python