在python中将两个列表分组
我有两个列表,我想根据列表的第一个元素对它们进行分组在python中将两个列表分组,python,list,Python,List,我有两个列表,我想根据列表的第一个元素对它们进行分组 list1 = [['1','abc','zef'],['2','qwerty','opo'],['3','lol','pop']] list2 = [['1','rofl','pole'],['2','sole','pop'],['3','lmao','wtf']] 这里,列表中的第一个元素是“1”、“2”和“3” 我希望我的最终列表如下所示:- Final_List = [['1', 'abc', 'zef', 'rofl', 'po
list1 = [['1','abc','zef'],['2','qwerty','opo'],['3','lol','pop']]
list2 = [['1','rofl','pole'],['2','sole','pop'],['3','lmao','wtf']]
这里,列表中的第一个元素是“1”、“2”和“3”
我希望我的最终列表如下所示:-
Final_List = [['1', 'abc', 'zef', 'rofl', 'pole'], ['3', 'lol', 'pop', 'lmao', 'wtf'], ['2', 'qwerty', 'opo', 'sole', 'pop']]
我已经用下面的代码尝试过了
#!/usr/bin/python
list1 = [['1','abc','zef'],['2','qwerty','opo'],['3','lol','pop']]
list2 = [['1','rofl','pole'],['2','sole','pop'],['3','lmao','wtf']]
d = {}
for i in list1:
d[i[0]] = i[1:]
for i in list2:
d[i[0]].extend(i[1:])
Final_List = []
for key, value in d.iteritems():
value.insert(0,key)
Final_List.append(value)
这段代码可以工作,但我想知道是否有一种更简单、更干净的方法可以做到这一点
有什么帮助吗?是的,有列表理解和枚举功能
list1 = [['1','abc','zef'],['2','qwerty','opo'],['3','lol','pop']]
list2 = [['1','rofl','pole'],['2','sole','pop'],['3','lmao','wtf']]
print [set(v + list2[k]) for k,v in enumerate(list1)]
[['1', 'abc', 'zef', 'rofl', 'pole'], ['2', 'qwerty', 'opo', 'sole', 'pop'], ['3', 'lol', 'pop', 'lmao', 'wtf']]
编辑
具有指数关系
list1 = [['1','abc','zef'],['2','qwerty','opo'],['3','lol','pop']]
list2 = [['1','rofl','pole'],['3','lmao','wtf'],['2','sole','pop']]
d1 = {a[0]:a for a in list1}
d2 = {a[0]:a for a in list2}
print [set(v + d2[k]) for k, v in d1.items()]
您的代码似乎是正确的。只需修改以下部分:
Final_List = []
for key in d:
L = [key] + [x for x in d[key]]
Final_List.append(L)
我会像你写的那样,稍加修改,像这样
d = {}
for items in (list1, list2):
for item in items:
d.setdefault(item[0], [item[0]]).extend(item[1:])
[['3', 'lol', 'pop', 'lmao', 'wtf'],
['1', 'abc', 'zef', 'rofl', 'pole'],
['2', 'qwerty', 'opo', 'sole', 'pop']]
使用默认的dict和list理解,您可以缩短代码
from collections import defaultdict
list1 = [['1','abc','zef'],['2','qwerty','opo'],['3','lol','pop']]
list2 = [['1','rofl','pole'],['2','sole','pop'],['3','lmao','wtf']]
d = defaultdict(list)
for i in list1 + list2:
d[i[0]].extend(i[1:])
Final_List = [[key] + value for key, value in d.iteritems()]
print Final_List
如果
Final_List
内列表中的项目顺序不重要,则可以使用该顺序
[list(set(sum(itm, []))) for itm in zip(list1, list2)]
使用xrange,您只能在列表中迭代一次。一点功能性风格:
import operator, itertools
from pprint import pprint
one = [['1','abc','zef'],['2','qwerty','opo'],['3','lol','pop']]
two = [['1','rofl','pole'],['2','sole','pop'],['3','lmao','wtf']]
一些助手:
zero = operator.itemgetter(0)
all_but_the_first = operator.itemgetter(slice(1, None))
data = (one, two)
def foo(group):
# group is (key, iterator) from itertools.groupby
key = group[0]
lists = group[1]
result = list(key)
for item in lists:
result.extend(all_but_the_first(item))
return result
函数来处理daa
def process(data, func = foo):
# concatenate all the sublists
new = itertools.chain(*data)
# group by item zero
three = sorted(new, key = zero)
groups = itertools.groupby(three, zero)
# iterator that builds the new lists
return itertools.imap(foo, groups)
用法
列表1=['1','abc','zef',['2','qwerty','opo',['3','lol','pop']] 列表2=['1'、'rofl'、'pole']、['2'、'sole'、'pop']、['3'、'lmao'、'wtf'] 最终清单=[] 对于范围(0,len(列表1))中的i: 打印最终用户列表 输出
[['3', 'lol', 'pop', 'lmao', 'wtf'],
['1', 'abc', 'zef', 'rofl', 'pole'],
['2', 'qwerty', 'opo', 'sole', 'pop']]
[[1]、[abc]、[zef]、[rofl]、[pole]、[2]、[qwerty]、[opo]、[sole]、[pop]、[3]、[lol]、[pop]、[lmao]、[wtf]、[p>前两个元素是否总是像您的示例一样并排完全相同?您的数据结构很奇怪。为什么每个列表项的第一个元素都有一个索引或键?像这样的问题可能更适合。您使用dicts更改数据结构的方式比发布的答案中建议的方式更简洁。您可以在版本中缩短的内容是使用列表理解。我很乐意听听我如何改进这篇文章。我没有投反对票,但有一个改进是
d.setdefault(item[0],[item[0]])。extend(item[1:])
然后要获得列表,就是d.values()
def process(data, func = foo):
# concatenate all the sublists
new = itertools.chain(*data)
# group by item zero
three = sorted(new, key = zero)
groups = itertools.groupby(three, zero)
# iterator that builds the new lists
return itertools.imap(foo, groups)
>>> pprint(list(process(data)))
[['1', 'abc', 'zef', 'rofl', 'pole'],
['2', 'qwerty', 'opo', 'sole', 'pop'],
['3', 'lol', 'pop', 'lmao', 'wtf']]
>>>
>>> for thing in process(data):
print thing
['1', 'abc', 'zef', 'rofl', 'pole']
['2', 'qwerty', 'opo', 'sole', 'pop']
['3', 'lol', 'pop', 'lmao', 'wtf']
>>>
Final_List.append(list1[i] + list2[i])
del Final_List[i][3]