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

2024-07-08 10:38:30 发布

您现在位置:Python中文网/ 问答频道 /正文

{'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"}]}

我想把这个dict的所有键和值作为一个列表(从dict或array的值中取出)

打印结果应如下:

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

Tags: nametestcomusernameactionpasswordmobilecard
1条回答
网友
1楼 · 发布于 2024-07-08 10:38:30

您可能需要使用递归函数来提取所有的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

这种东西。我来自C++背景,所以我不得不为所有的谷歌语法。

相关问题 更多 >

    热门问题