gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:31:48 24 4
gpt4 key购买 nike

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

我想把这个字典的所有键和值作为一个列表(从字典或数组的值中取出)

打印结果应该是这样的:

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

最佳答案

您可能希望使用递归函数来提取所有键、值 对。

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++ 背景,所以我不得不通过谷歌搜索所有语法。

关于python - 如何获取字典列表和字典的嵌套字典中的所有键和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10569636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com