2012-03-02 63 views
0

我想知道如何靜態初始化詞典列表 - 是這樣的:初始化字典的列表值

我本來像這樣的列表:

consumers = ['aserver.foo.com','anotherserver.foo.com', 
      'thirdserver.foo.com','lastserver.foo.com'] 

但我想有一個結構,我可以解決這樣的:

consumers = [ 
     'aserver'{ 
      'hostname'='aserver.foo.com', 
      'binddn'=masterbinddn, 
      'bindpw'=masterbindpw 
      }, 
     'anotherserver'{ 
      'hostname'='anotherserver.foo.com', 
      'binddn'=masterbinddn, 
      'bindpw'=masterbindpw 
     }, 
     'athirdserver'{ 
      'hostname'='athirdserver.foo.com', 
      'binddn'=targetbinddn, 
      'bindpw'=targetbindpw 
     }, 
     'lastserver'{ 
      'hostname'='lastserver.foo.com', 
      'binddn'=targetbinddn, 
      'bindpw'=targetbindpw 
     } 
    ] 

的想法是,我可以這樣做:

for server in consumers: 
    do_something_to_server(server['hostname'], server['binddn'], server['bindpw']) 

我在吠叫錯誤的樹,還是錯過了一些基本的東西?

+0

所以你爲什麼認爲你不能這樣做? – 2012-03-02 16:01:40

回答

5

下面創建一個字典列表:

consumers = [ 
    { 
     'hostname': 'aserver.foo.com', 
     'binddn': masterbinddn, 
     'bindpw': masterbindpw 
    }, 
    { 
     'hostname': 'anotherserver.foo.com', 
     'binddn': masterbinddn, 
     'bindpw': masterbindpw 
    }, 
    { 
     'hostname': 'athirdserver.foo.com', 
     'binddn': targetbinddn, 
     'bindpw': targetbindpw 
    }, 
    { 
     'hostname': 'lastserver.foo.com', 
     'binddn': targetbinddn, 
     'bindpw': targetbindpw 
    }, 
] 

然後,您可以遍歷它像這樣:

for server in consumers: 
    do_something_to_server(server['hostname'], server['binddn'], server['bindpw']) 
+0

這在語法上不合法。 – Marcin 2012-03-02 16:02:27

+1

@Marcin:你確定你正在查看答案的當前版本,而不是一些中間編輯?它在我的Python解釋器中工作正常。 – NPE 2012-03-02 16:03:14

+0

是的,現在你已經改變了它。 – Marcin 2012-03-02 16:06:27

0

你的例子數據結構是不是有效的Python:你不能有串緊接着是字典。

但要獲得字典的簡單列表,這會工作:

consumers = ['aserver.foo.com','anotherserver.foo.com', 
      'thirdserver.foo.com','lastserver.foo.com'] 

consumer_dict = [{'hostname': consumer, 'binddn': targetbinddn, 'bindpw': targetbindpw} 
       for consumer in consumers 
1

只要看看你的結構:

consumers = { 
    'aserver': { 
     'hostname':'aserver.foo.com', 
     'binddn':masterbinddn, 
     'bindpw':masterbindpw 
     }, 
    'anotherserver': { 
     'hostname':'anotherserver.foo.com', 
     'binddn':masterbinddn, 
     'bindpw':masterbindpw 
    }, 
    'athirdserver': { 
     'hostname':'athirdserver.foo.com', 
     'binddn':targetbinddn, 
     'bindpw':targetbindpw 
    }, 
    'lastserver': { 
     'hostname':'lastserver.foo.com', 
     'binddn':targetbinddn, 
     'bindpw':targetbindpw 
    } 
} 

並更改與:

for server in consumers.values(): 
    do_something_to_server(server['hostname'], server['binddn'], server['bindpw']) 
+0

Thx,複製粘貼和忘記:D – Bogdan 2012-03-02 16:11:27

1

它聽起來像是你想要一本字典的字典,或者爲你的字典添加另一個字段來捕獲你的名字把它們放在你的僞代碼中。

此外,您遇到的主要問題是您的字典語法錯誤。字面上的解釋是:

{ 'key': 'value'} 

# or 

dict(key = 'value') 

# not 

{'key' = 'value'} 
1

您可以使用小更高效namedtuple

from collections import namedtuple 
server = namedtuple('server', ('hostname','binddn','bindpw')) 

consumers = [ 
    server(
     hostname = 'aserver.foo.com', 
     binddn = masterbinddn, 
     bindpw = masterbindpw 
      ), 
    server(
     hostname = 'anotherserver.foo.com', 
     binddn = masterbinddn, 
     bindpw = masterbindpw 
      ), 
    server(
     hostname = 'athirdserver.foo.com', 
     binddn = targetbinddn, 
     bindpw = targetbindpw 
      ), 
    server(
     hostname = 'lastserver.foo.com', 
     binddn = targetbinddn, 
     bindpw = targetbindpw 
      ), 
    ] 

而且你的循環變化:

for consumer in consumers: 
    do_something_to_server(consumer.hostname, consumer.binddn, consumer.bindpw) 

甚至:

for consumer in consumers: 
     do_something_to_server(*consumer)