我有两个列表,我想根据列表的第一个元素进行分组。
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)
此代码有效,但我想知道是否有一种简单易用的方法
任何帮助?
答案 0 :(得分:5)
我会像你写的那样写一些修改,比如这个
准备一个字典,其中包含与第一个元素对应的第二个位置的所有元素。
d = {}
for items in (list1, list2):
for item in items:
d.setdefault(item[0], [item[0]]).extend(item[1:])
然后只需从字典中获取所有值(感谢@jamylak): - )
print(d.values())
<强>输出强>
[['3', 'lol', 'pop', 'lmao', 'wtf'],
['1', 'abc', 'zef', 'rofl', 'pole'],
['2', 'qwerty', 'opo', 'sole', 'pop']]
答案 1 :(得分:1)
如果Final_List
内的列表中的项目顺序不重要,则可以使用此项,
[list(set(sum(itm, []))) for itm in zip(list1, list2)]
答案 2 :(得分:0)
是的,列表理解和枚举
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()]
答案 3 :(得分:0)
您的代码似乎正确无误。只需修改以下部分:
Final_List = []
for key in d:
L = [key] + [x for x in d[key]]
Final_List.append(L)
答案 4 :(得分:0)
使用默认字典和列表推导可以缩短代码
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
答案 5 :(得分:0)
list3 = []
for i in xrange(0,max(len(list1[0]), len(list2[0]))):
list3.append(list(list1[i]))
list3[i].extend(x for x in list2[i] if x not in list3[i])
使用xrange,您只能在列表中迭代一次。
答案 6 :(得分:0)
一些功能风格:
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)
用法
>>> 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']
>>>
答案 7 :(得分:0)
list1 = [[&#39; 1&#39;,&#39; abc&#39;,&#39; zef&#39;],[&#39; 2&#39;,&#39; qwerty& #39;,&#39; OPO&#39;],[&#39; 3&#39;&#39;洛尔&#39;&#39;弹出&#39;]]
list2 = [[&#39; 1&#39;,&#39; rofl&#39;,&#39; pole&#39;],[&#39; 2&#39;,&#39; sole& #39;,&#39;弹出&#39;],[&#39; 3&#39;&#39; LMAO&#39;&#39;跆拳道&#39;]]
Final_List = []
表示范围内的i(0,len(list1)):
Final_List.append(list1[i] + list2[i])
del Final_List[i][3]
打印Final_List
<强>输出强>
[[&#39; 1&#39;,&#39; abc&#39;,&#39; zef&#39;,&#39; rofl&#39;,&#39; pole&#39;] ,[&#39; 2&#39;,&#39; qwerty&#39;,&#39; opo&#39;,&#39; sole&#39;,&#39; pop&#39;],[& #39; 3&#39;,&#39; lol&#39;,&#39; pop&#39;,&#39; lmao&#39;,&#39; wtf&#39;]]