在python中对内部列表元素的列表列表进行分组

时间:2014-08-02 12:17:26

标签: python list

我有一个列表如:

List = [['Server1','Server2','Server4','Server6','EnvName1','become_a'],
        ['Server2','Server3','Server1','Server5','EnvName2','become_a'],
        ['Server4','Server3','Server7','Server6','EnvName3','become_b'],
        ['Server5','Server1','Server2','Server3','EnvName4','become_a'],
        ['Server4','Server2','Server1','Server7','EnvName5','become_b']]

我的列表列表包含的是服务器的名称,运行它的环境以及成为用于对它们执行ssh的帐户,我想在它们上运行一些shell脚本但我不想做多余的ssh针对不同环境的连接。所以我想把它们分组。我的分组应该像所有具有相同服务器的EnvNames并且成为帐户应该是列表中的组,以便我只在该服务器上执行ssh一次,如果两个EnvNames服务器相同但是成为帐户不同,那么我希望它们是在另一个列表中。

我的最终名单应该是这样的:

Final_List = [['Server1','EnvName1,EnvName2,EnvName4','become_a'],
              ['Server1','Envname5','become_b'],
              ['Server2','EnvName1,Envname2','become_a'],
              ['Server2','EnvName5','become_b'],
              ['Server3','EnvName2,EnvName4','become_a'],
              ['Server3','EnvName3','become_b'],
              ['Server4','EnvName1','become_a'],
              ['Server4','EnvName3,EnvName5','become_b'],
              ['Server5','EnvName5','become_a'],
              ['Server6','EnvName1','become_a'],
              ['Server6','EnvName3','become_b'],
              ['Server7','EnvName3,EnvName5','become_b']]

我试图将它们分组,但它出现了可怕的错误

1 个答案:

答案 0 :(得分:1)

您想要对服务器和帐户名称进行分组。

使用字典进行分组,键入元组:

grouped = {}
for entry in List:
    env, account = entry[-2:]
    for server in entry[:-2]:
        key = server, account
        grouped.setdefault(key, []).append(env)

分组后,您可以列出最终输出:

Final_List = [[server] + environments + [account]
              for (server, account), environments in sorted(grouped.items())]

这为环境字符串生成单独的条目。您可以将这些字符串放在一个字符串中:

Final_List = [[server, ','.join(environments), account]
              for (server, account), environments in sorted(grouped.items())]

演示:

>>> List = [['Server1','Server2','Server4','Server6','EnvName1','become_a'],
...         ['Server2','Server3','Server1','Server5','EnvName2','become_a'],
...         ['Server4','Server3','Server7','Server6','EnvName3','become_b'],
...         ['Server5','Server1','Server2','Server3','EnvName4','become_a'],
...         ['Server4','Server2','Server1','Server7','EnvName5','become_b']]
>>> grouped = {}
>>> for entry in List:
...     env, account = entry[-2:]
...     for server in entry[:-2]:
...         key = server, account
...         grouped.setdefault(key, []).append(env)
... 
>>> [[server] + environments + [account]
...               for (server, account), environments in sorted(grouped.items())]
[['Server1', 'EnvName1', 'EnvName2', 'EnvName4', 'become_a'],
 ['Server1', 'EnvName5', 'become_b'],
 ['Server2', 'EnvName1', 'EnvName2', 'EnvName4', 'become_a'],
 ['Server2', 'EnvName5', 'become_b'],
 ['Server3', 'EnvName2', 'EnvName4', 'become_a'],
 ['Server3', 'EnvName3', 'become_b'],
 ['Server4', 'EnvName1', 'become_a'],
 ['Server4', 'EnvName3', 'EnvName5', 'become_b'],
 ['Server5', 'EnvName2', 'EnvName4', 'become_a'],
 ['Server6', 'EnvName1', 'become_a'],
 ['Server6', 'EnvName3', 'become_b'],
 ['Server7', 'EnvName3', 'EnvName5', 'become_b']]
>>> [[server, ','.join(environments), account]
...               for (server, account), environments in sorted(grouped.items())]
[['Server1', 'EnvName1,EnvName2,EnvName4', 'become_a'],
 ['Server1', 'EnvName5', 'become_b'],
 ['Server2', 'EnvName1,EnvName2,EnvName4', 'become_a'], 
 ['Server2', 'EnvName5', 'become_b'],
 ['Server3', 'EnvName2,EnvName4', 'become_a'],
 ['Server3', 'EnvName3', 'become_b'],
 ['Server4', 'EnvName1', 'become_a'],
 ['Server4', 'EnvName3,EnvName5', 'become_b'],
 ['Server5', 'EnvName2,EnvName4', 'become_a'],
 ['Server6', 'EnvName1', 'become_a'],
 ['Server6', 'EnvName3', 'become_b'],
 ['Server7', 'EnvName3,EnvName5', 'become_b']]