2012-05-13 2 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"}]} 

私は(彼らは辞書や配列されている値のうち)リストとして、この辞書の&値list-of-dictsとdictsのネストされたdictのすべてのキーと値を取得する方法は?

印刷結果は次のようにする必要がありますすべてのキーを取得したい:

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

完全性を保つためには、 'elif isinstance(value、str):'の代わりに 'else:'を使うべきです。 – 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 + +の背景から来たので、私はすべての構文のためにgoogleする必要がありました。

+0

'elif isinstance(value、unicode):'を 'else:'と置き換える方が良いかもしれません。 – SparkAndShine

+0

@sparkandshine IMHO明示的は暗黙的よりも優れています。 'isinstance'はドキュメントとしても機能します。 – Hindol

+0

リストで実際には動作しません(作者の例でもそうです)?リストを横断しなければならない。 – antonavy