2012-05-13 79 views
10
{'action_name':'mobile signup', 
    'functions':[{'name':'test_signUp', 
        'parameters':{'username':'[email protected]', 
           '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 : [email protected] 
password : 12345 
mobileLater: 123454231 
mobile : 1e2w1e2w 
card : 1232313 
cardLater : 1234321234321 
MOB_header : My stores 
+0

也許[this](http://stackoverflow.com/questions/1679384/converting-python-dictionary-to-list)會有幫助嗎? – Hindol

+0

你有什麼嘗試?向我們展示一些代碼。 – Vikas

+0

對於完整的一般性,您應該使用'else:'而不是'elif isinstance(value,str):'。 – huon

回答

8

你可能想要使用遞歸函數來提取所有的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++的背景,所以我不得不穀歌的所有語法。

+0

用'else:'替換'elif isinstance(value,unicode):'行可能會更好。 – SparkAndShine

+0

@sparkandshine恕我直言,明確比隱含更好。 'isinstance'部分也可以作爲文檔。 – Hindol

+0

真的不能和列表一起工作(即使在作者的例子中),是嗎?也必須遍歷列表。 – antonavy