Ben
Ben

Reputation: 391

String building in python from lists

 [['Demo-Site', '10.227.209.139'], ['Demo-Site', '10.227.215.68'], 
['Demo-Site', '172.18.74.146'], ['Site', '10.152.114.65'], 
['Site', '10.227.211.244'], ['Demo-Site', '10.227.147.98'], 
 ['test', '172.18.74.146']]

How can I concatenate all of the IP's to form a big string based on if the first index is the same? Do I make a default dictionary?

Should be:

["Site", "10.227.211.244, 10.152.114.65"]

Upvotes: 2

Views: 79

Answers (5)

Sede
Sede

Reputation: 61225

It seems like you want to group your record based on the first element in each sub-list. That is what the groupby does. but there is an important preliminary step which is sorting your list based on the first first element in each sub-list. You can do this using the sorted function and use itemgetter as key function.

from operator import itemgetter
from itertools import groupby
result = []
my_list = [['Demo-Site', '10.227.209.139'], 
['Demo-Site', '10.227.215.68'], 
['Demo-Site', '172.18.74.146'], 
['Site', '10.152.114.65'], 
['Site', '10.227.211.244'], 
['Demo-Site', '10.227.147.98'], 
['test', '172.18.74.146']]

Demo groupby

for g, data in groupby(sorted(my_list, key=itemgetter(0)), itemgetter(0)):
     print(g)
     for elt in data:
         print(' ', elt)

yields:

Demo-Site
  ['Demo-Site', '10.227.209.139']
  ['Demo-Site', '10.227.215.68']
  ['Demo-Site', '172.18.74.146']
  ['Demo-Site', '10.227.147.98']
Site
  ['Site', '10.152.114.65']
  ['Site', '10.227.211.244']
test
  ['test', '172.18.74.146']

As you can see your data is grouped by first element in your sub-list. So all you need now is concatenate (.join) the last elements of the "members" of the same group and then append a list [<given group>, <members string>] to the result list.

>>> for g, data in groupby(sorted(my_list, key=itemgetter(0)), itemgetter(0)):
...     result.append([g, ', '.join(elt[1] for elt in data)])
... 
>>> result
[['Demo-Site', '10.227.209.139, 10.227.215.68, 172.18.74.146, 10.227.147.98'], ['Site', '10.152.114.65, 10.227.211.244'], ['test', '172.18.74.146']]

Upvotes: 1

hiro protagonist
hiro protagonist

Reputation: 46859

or, as you mentioned, with defaultdict:

from collections import defaultdict

lst = [['Demo-Site', '10.227.209.139'], ['Demo-Site', '10.227.215.68'], 
['Demo-Site', '172.18.74.146'], ['Site', '10.152.114.65'], 
['Site', '10.227.211.244'], ['Demo-Site', '10.227.147.98'], 
 ['test', '172.18.74.146']]

dct = defaultdict(list)

for name, ip in lst:
    dct[name].append(ip)

res = [ [name, ', '.join(ips)] for name, ips in dct.items() ]
print(res)

Upvotes: 0

Anshul Goyal
Anshul Goyal

Reputation: 76887

Here you go, using a defaultdict based solution:

In [1]: list_of_ips = [['Demo-Site', '10.227.209.139'], ['Demo-Site', '10.227.215.68'], 
   ...: ['Demo-Site', '172.18.74.146'], ['test', '10.152.114.65'], 
   ...: ['Site', '10.227.211.244'], ['Demo-Site', '10.227.147.98'], 
   ...:  ['test', '172.18.74.146']]

In [2]: from collections import defaultdict

In [3]: resp_dict = defaultdict(list)

In [4]: for item in list_of_ips:
   ...:     resp_dict[item[0]].append(item[1])
   ...:     

In [5]: result = [[key, ", ".join(resp_dict[key])] for key in resp_dict]

In [6]: result
Out[6]: 
[['test', '10.152.114.65, 172.18.74.146'],
 ['Site', '10.227.211.244'],
 ['Demo-Site', '10.227.209.139, 10.227.215.68, 172.18.74.146, 10.227.147.98']]

Upvotes: 1

Kasravnd
Kasravnd

Reputation: 107287

You can use a dictionary (and dict.setdefault method )to preserve your items based on first index,then join the values :

>>> li=[['Demo-Site', '10.227.209.139'], ['Demo-Site', '10.227.215.68'], 
... ['Demo-Site', '172.18.74.146'], ['Site', '10.152.114.65'], 
... ['Site', '10.227.211.244'], ['Demo-Site', '10.227.147.98'], 
...  ['test', '172.18.74.146']]
>>> 
>>> d={}
>>> 
>>> for i,j in li:
...   d.setdefault(i,[]).append(j)
... 
>>> [[i,','.join(j)] for i,j in d.items()]
[['test', '172.18.74.146'], ['Site', '10.152.114.65,10.227.211.244'], ['Demo-Site', '10.227.209.139,10.227.215.68,172.18.74.146,10.227.147.98']]
>>> 

Upvotes: 0

The6thSense
The6thSense

Reputation: 8335

You could use normal dictionary setdefault method :

dic={}
for ele in lis:
    dic.setdefault(ele[0],[]).append(ele[1])
[[a,','.join(b)] for a,b in dic.items()]

Upvotes: 1

Related Questions