簡體   English   中英

如何定義一個Python字典,其中多個子字典具有相同的值?

[英]How to define a Python dict with more than one sub-dict having the same values?

我正在嘗試創建一個Python dict其成員本身就是dict ,其中兩個是相同的:

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服務器定義相同,但是我不想重復它-有什么方法可以做到這一點?

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

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']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM