[['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']]
如果第一个索引是相同的,我如何连接所有IP以形成一个大字符串?我是否制作了默认词典?
应该是:
["Site", "10.227.211.244, 10.152.114.65"]
答案 0 :(得分:1)
您可以使用普通字典setdefault
方法:
dic={}
for ele in lis:
dic.setdefault(ele[0],[]).append(ele[1])
[[a,','.join(b)] for a,b in dic.items()]
答案 1 :(得分:1)
您可以使用基于defaultdict
的解决方案:
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']]
答案 2 :(得分:1)
您似乎希望根据每个子列表中的第一个元素分组您的记录。这就是groupby
的作用。但是有一个重要的初步步骤,它根据每个子列表中的第一个第一个元素对列表进行排序。您可以使用sorted
功能执行此操作,并使用itemgetter
作为键功能。
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']]
演示groupby
for g, data in groupby(sorted(my_list, key=itemgetter(0)), itemgetter(0)):
print(g)
for elt in data:
print(' ', elt)
的产率:
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']
如您所见,您的数据按子列表中的第一个元素分组。所以你现在需要的是连接(.join
)&#34;成员&#34;的最后一个元素。相同的组,然后将列表[<given group>, <members string>]
附加到result
列表。
>>> 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']]
答案 3 :(得分:0)
您可以使用字典(和dict.setdefault
方法)根据第一个索引保留您的项目,然后加入值:
>>> 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']]
>>>
答案 4 :(得分:0)
或者,正如您所提到的,使用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)