2012-03-02 57 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)