0

I have a list of list

List = [['ServerA','Envname1','abc','xyz'],
        ['ServerA','Envname2','abc','xyz'],
        ['ServerB','Envname3','aaa','bbb'],
        ['ServerC','EnvName4','uuu','vvv'],
        ['ServerB','Envname5','aaa','bbb']]

If the server is same then the values at 2nd and 3rd index of the inner lists will be same.

For example:- In the 1st list ['ServerA','Envname1','abc','xyz'] and 2nd list ['ServerA','Envname2','abc','xyz'] server is same ('ServerA') so the values 'abc' and 'xyz' is same.

Likewise, values for ServerB in the 3rd and 4th is same 'aaa' and 'bbb'

Only the Envname is changing even if the server is same.

What i want in my final list of list is to group all the servers which has the same name and add all the Envnames in them accordingly, rest of the elements will be the same if the server name is same.

Expected List

Final_list = [['ServerA','Envname1,Envname2','abc','xyz'],
              ['ServerB','Envname3,Envname5','aaa','bbb'],
              ['ServerC','EnvName4','uuu','vvv']]

Can anybody advise me on this?

0

2 Answers 2

2

This should work:

l = [['ServerA','Envname1','abc','xyz'],
     ['ServerA','Envname2','abc','xyz'],
     ['ServerB','Envname3','aaa','bbb'],
     ['ServerC','EnvName4','uuu','vvv'],
     ['ServerB','Envname5','aaa','bbb'],
     ]

d = {}
for item in l:
    key = (item[0], item[2], item[3])
    env = d.setdefault(key, [])
    env.append(item[1])

final_list = [[k[0], ",".join(v), k[1], k[2]] for k, v in d.items()]
print final_list

Output:

[['ServerA', 'Envname1,Envname2', 'abc', 'xyz'],
 ['ServerB', 'Envname3,Envname5', 'aaa', 'bbb'],
 ['ServerC', 'EnvName4', 'uuu', 'vvv'],
 ]
Sign up to request clarification or add additional context in comments.

Comments

0
from itertools import groupby,chain
from operator import itemgetter
List_server = [['ServerA','Envname1','abc','xyz'],
        ['ServerA','Envname2','abc','xyz'],
        ['ServerB','Envname3','aaa','bbb'],
        ['ServerC','EnvName4','uuu','vvv'],
        ['ServerB','Envname5','aaa','bbb']]

List_server=sorted(List,key=lambda x:x[0][-1])
print [set(chain(*list(g))) for i,g in groupby(List_server,key=itemgetter(0))]

output: [set(['xyz', 'abc', 'ServerA', 'Envname2', 'Envname1']),
         set(['Envname5', 'aaa', 'bbb', 'ServerB', 'Envname3']), 
         set(['EnvName4', 'uuu', 'vvv', 'ServerC'])]

explantion:

  1. First sort your list according to servers List_server=sorted(List,key=lambda x:x[0][-1])
  2. Perform Groupby by using key=itemgetter(0)

Note: Do not use Python Keywords as variables

1 Comment

The OP asked that the env names be joined with commas. You are also not preserving the ordering of the sub-lists, which might be important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.