Skip to main content
 首页 » 编程设计

python之从字典中删除嵌套键

2024年12月31日8powertoolsteam

所以我有一个字典如下:

{ 
    'assignees': { 
        'arrayValue': { 
            'values': [{ 
                'stringValue': 
 
                    '56ea94b3d517f047c9d680a7' 
            }] 
        } 
    }, 
    'assigneesMap': { 
        'mapValue': { 
            'fields': { 
                '56ea94b3d517f047c9d680a7': { 
                    'booleanValue': True 
                } 
            } 
        } 
    }, 
    'closed': { 
        'booleanValue': False 
    }, 
    'color': { 
        'stringValue': '#ebbdf9' 
    }, 
    'createdDate': { 
        'timestampValue': '2018-12-07T06:11:40.058Z' 
    }, 
    'creator': { 
        'stringValue': '56ea94b3d517f047c9d680a7' 
    }, 
    'deleted': { 
        'booleanValue': False 
    }, 
    'endDate': { 
        'nullValue': None 
    }, 
    'lastUpdated': { 
        'timestampValue': '2018-12-07T06:11:40.058Z' 
    }, 
    'name': { 
        'stringValue': 'Test Checklist Tasks' 
    }, 
    'priority': { 
        'integerValue': '1' 
    }, 
    'projectId': { 
        'stringValue': 'M919Bcgv0h4J76VdQHYX' 
    }, 
    'status': { 
        'stringValue': 'created' 
    }, 
    'tags': { 
        'arrayValue': {} 
    }, 
    'users': { 
        'arrayValue': { 
            'values': [{ 
                'stringValue': '56ea94b3d517f047c9d680a7' 
            }] 
        } 
    }, 
    'usersRole': { 
        'arrayValue': { 
            'values': [{ 
                'mapValue': { 
                    'fields': { 
                        'role': { 
                            'stringValue': 'admin' 
                        }, 
                        'userId': { 
                            'stringValue': '56ea94b3d517f047c9d680a7' 
                        } 
                    } 
                } 
            }] 
        } 
    } 
} 

我需要删除不需要的键(类型信息)以产生如下结果:

{ 
    'assignees': ['56ea94b3d517f047c9d680a7'], 
    'assigneesMap': {'56ea94b3d517f047c9d680a7': True}, 
    'closed': False, 
    'color': '#ebbdf9', 
    'createdDate': '2018-12-07T06:11:40.058Z', 
    'creator': '56ea94b3d517f047c9d680a7', 
    'deleted': False, 
    'endDate': None, 
    'lastUpdate': '2018-12-07T06:11:40.058Z', 
    'name': 'Test Checklist Tasks', 
    'priority': 1, 
    'projectId': 'M919Bcgv0h4J76VdQHYX', 
    'status': 'created', 
    'tags': [], 
    'users': ['56ea94b3d517f047c9d680a7'], 
    'usersRole': [{'role': 'admin', 'userId': '56ea94b3d517f047c9d680a7'}] 
} 

我能想到的解决此问题的一种方法是保留字段名称和字段类型的映射并采取相应措施。

{ 
    'assignees': 'array_of_strings', 
    'assigneesMap': 'map', 
    'closed': 'boolean', 
    ..... 
} 

有没有更好的方法在不使用任何此类配置的情况下执行此操作?也许使用递归?

请您参考如下方法:

递归绝对有可能,因为它遵循数据结构中的模式,并且不需要映射字段,如果引入其他元素,映射字段可能会变得不正确。

这是一个用于处理 arrayValue 部分的代码片段,可以针对其余部分进行增强。

def parseValue(t): 
    t2 = '' 
    for k in t.keys(): 
        v = t[k] 
        if k == 'arrayValue': 
            t2 = parseValue(v) 
        elif k == 'values': 
            t2 = [] 
            for k2 in v: 
                t2.append(parseValue(k2)) 
        elif k == 'stringValue': 
            t2 = v 
    return t2 
 
 
e = {} 
for k, v in input.iteritems(): 
    e[k] = parseValue(v) 
print e