从字典中删除嵌套键

时间:2018-12-07 10:47:52

标签: python dictionary recursion

所以我有如下字典:

{
    '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',
    .....
}

是否有更好的方法可以执行此操作而不使用任何此类配置?也许使用递归?

1 个答案:

答案 0 :(得分:1)

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

这是用于处理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
相关问题