2017-03-09 2 views
2

私はそのメンバー自身dictのあるPythonのdictを作成しようとしています、そのうちの2つが同じです:同じ値を持つ複数のサブディクテーションを持つPythonディクテーションを定義するにはどうすればよいですか?

servers = { 
    "test": { 
    "auth_token": "some-auth-token", 
    "client_id": "some-client-id", 
    "client_secret": "some-client-secret", 
    "scope": "some-scope" 
    }, 
    "live": { 
    "auth_token": "auth-token-for-live", 
    "client_id": "client-id-for-live", 
    "client_secret": "client-secret-for-live", 
    "scope": "scope-for-live" 
    }, 
    "demo": servers["test"] # this doesn't work, but this is what I need 
} 

マイdemoサーバー定義は、私のtestサーバー定義と同じであるが、私はしないでください私はこれをやることができる方法はありますか?

+0

ない可能単一のステートメント – timgeb

+0

での私のデモ設定=> 'サーバー[「デモ」] [「client_secret」] =「foo'' =>の着信スタックオーバーフローの質問」私のテストにいくつかの変更をしてみましょうサーバーは突然変化せずに失敗します、なぜですか?私は本当にコピー貼りが悪いものとしてここに表示されません。 – polku

+0

テストキーとライブキーで最初にdictを作成できますか?その後、デモをキーにしてdictを更新し、値としてテストしてください: 'servers.update({'demo':servers ['test']})' –

答えて

0
x = { 
    "auth_token": "some-auth-token", 
    "client_id": "some-client-id", 
    "client_secret": "some-client-secret", 
    "scope": "some-scope" 
} 

servers = { 
    "test": x, 

    "live": { 
    "auth_token": "auth-token-for-live", 
    "client_id": "client-id-for-live", 
    "client_secret": "client-secret-for-live", 
    "scope": "scope-for-live" 
    }, 

    "demo": x 

} 

https://repl.it/GO6Y/0

+0

もちろん、私はそれを行うことができます。私は 'servers'自体の定義の中でそうすることができるかどうか疑問に思っていました。 –

+0

「Xはどうやってやるの?」 「このように」「もちろん」ということは、実際には別の質問をしていましたが、その理由を秘密にしておきます。 – TessellatingHeckler

0
servers = { 
    "test": { 
    "auth_token": "some-auth-token", 
    "client_id": "some-client-id", 
    "client_secret": "some-client-secret", 
    "scope": "some-scope" 
    }, 
    "live": { 
    "auth_token": "auth-token-for-live", 
    "client_id": "client-id-for-live", 
    "client_secret": "client-secret-for-live", 
    "scope": "scope-for-live" 
    }} 

servers['demo'] = servers['test'] 
+0

「テスト」を変更すると「デモ」が自動的に更新されます。 – CaptainKinematics

関連する問題