简体   繁体   English

如何在字典列表和字典的嵌套字典中获取所有键和值?

[英]how to get all keys&values in nested dict of list-of-dicts and dicts?

{'action_name':'mobile signup',
    'functions':[{'name':'test_signUp',
                  'parameters':{'username':'max@getappcard.com',
                                'password':'12345',
                                'mobileLater':'123454231',
                                'mobile':'1e2w1e2w',
                                'card':'1232313',
                                'cardLater':'1234321234321'}}],
    'validations':[
            {'MOB_header':'My stores'},
            {'url':"/stores/my"}]}

I want to get all the keys & values of this dict as a list (out of values that they are dict or array)我想将这个 dict 的所有键和值作为列表(从它们是 dict 或数组的值中获取)

print result should be like this:打印结果应该是这样的:

action name = mobile signup
name = test_signUp
username : max@getappcard.com
password : 12345
mobileLater: 123454231
mobile : 1e2w1e2w
card : 1232313 
cardLater : 1234321234321
MOB_header : My stores

You might want to use a recursive function to extract all the key, value pairs.您可能希望使用递归函数来提取所有key, value对。

def extract(dict_in, dict_out):
    for key, value in dict_in.iteritems():
        if isinstance(value, dict): # If value itself is dictionary
            extract(value, dict_out)
        elif isinstance(value, unicode):
            # Write to dict_out
            dict_out[key] = value
    return dict_out

Something of this sort.这种东西。 I come from C++ background so I had to google for all the syntaxes.我来自 C++ 背景,所以我不得不谷歌搜索所有语法。

a little late but for python 3.8 you can use yield from有点晚了,但对于 python 3.8,你可以使用yield from

def dictitems2list(d):
    for k, v in d.items():
        yield k
        if isinstance(v, dict):
            yield from get_all_items(v)
        else:
            yield v

all_itemt = list(dict2items(d))

I have modified a little bit from this link to get all keys&values in nested dict of list-of-dicts and dicts:我已经从这个链接中修改了一点,以获取字典列表和字典的嵌套字典中的所有键和值:

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            yield (key, value)
            yield from recursive_items(value)
        elif type(value) is list:
            yield (key, value)
            for i in value:
                if type(i) is dict:
                    yield from recursive_items(i)
        else:
            yield (key, value)

for i in recursive_items(your_dict):
    print(i) #print out tuple of (key, value)

Output:输出:

('action_name', 'mobile signup')
('functions', [{'name': 'test_signUp', 'parameters': {'username': 
'max@getappcard.com', 'password': '12345', 'mobileLater': '123454231', 'mobile': 
'1e2w1e2w', 'card': '1232313', 'cardLater': '1234321234321'}}])
('name', 'test_signUp')
('parameters', {'username': 'max@getappcard.com', 'password': '12345', 
'mobileLater': '123454231', 'mobile': '1e2w1e2w', 'card': '1232313', 
'cardLater': '1234321234321'})
('username', 'max@getappcard.com')
('password', '12345')
('mobileLater', '123454231')
('mobile', '1e2w1e2w')
('card', '1232313')
('cardLater', '1234321234321')
('validations', [{'MOB_header': 'My stores'}, {'url': '/stores/my'}])
('MOB_header', 'My stores')
('url', '/stores/my')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM