初始化字典值列表

Aar*_*ett 0 python

我想知道如何静态初始化字典列表 - 如下所示:

最初,我有一个这样的列表:

consumers = ['aserver.foo.com','anotherserver.foo.com',
             'thirdserver.foo.com','lastserver.foo.com']
Run Code Online (Sandbox Code Playgroud)

但是我希望有一个我能解决的结构:

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
        }
    ]
Run Code Online (Sandbox Code Playgroud)

这个想法是我可以做的事情:

for server in consumers:
    do_something_to_server(server['hostname'], server['binddn'], server['bindpw'])
Run Code Online (Sandbox Code Playgroud)

我是在咆哮错误的树,还是只是遗漏了一些基本的东西?

NPE*_*NPE 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
    },
]
Run Code Online (Sandbox Code Playgroud)

然后你可以这样迭代它:

for server in consumers:
    do_something_to_server(server['hostname'], server['binddn'], server['bindpw'])
Run Code Online (Sandbox Code Playgroud)