如何获取所有关键字和值的列表和列表的嵌套字典? [英] how to get all keys&values in nested dict of list-of-dicts and dicts?
本文介绍了如何获取所有关键字和值的列表和列表的嵌套字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
'functions':[{'name':'test_signUp',
'parameters':{'用户名':'max@getappcard.com',
'密码':'12345',
'mobileLater':'123454231',
'mobile':'1e2w1e2w',
'card':'1232313',
'cardLater':'1234321234321'}}],
'validations':[
{'MOB_header':'我的商店'},
{'url':/ stores / my}]}
钥匙&这个dict的值作为列表(超出他们是dict或array的值)
打印结果应该是这样的:
action name = mobile signup
name = test_signUp
用户名:max@getappcard.com
密码:12345
mobileLater: 123454231
手机:1e2w1e2w
卡:1232313
cardLater:1234321234321
MOB_header:我的商店
解决方案
您可能想使用递归函数来提取所有的键值
/ p>
def extract(dict_in,dict_out):
为key,在dict_in.iteritems()中的值:
如果isinstance(value,dict):#如果值本身是字典
extract(value,dict_out)
elif isinstance(value,unicode):
#写入dict_out
dict_out [ key] = value
return dict_out
这样的东西。我来自C ++背景,所以我必须google所有的语法。
{'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)
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.
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.
这篇关于如何获取所有关键字和值的列表和列表的嵌套字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文