可能重复:
Flattening a shallow list in Python
Making a flat list out of list of lists in Python
Merge two lists in python?
快速而简单的问题:
我如何合并。
[['a','b','c'],['d','e','f']]
到此:
['a','b','c','d','e','f']
答案 0 :(得分:20)
使用列表理解:
ar = [['a','b','c'],['d','e','f']]
concat_list = [j for i in ar for j in i]
答案 1 :(得分:12)
列表连接只是使用+
运算符完成的。
所以
total = []
for i in [['a','b','c'],['d','e','f']]:
total += i
print total
答案 2 :(得分:5)
这样做:
a = [['a','b','c'],['d','e','f']]
reduce(lambda x,y:x+y,a)
答案 3 :(得分:2)
尝试:
sum([['a','b','c'], ['d','e','f']], [])
或更长但更快:
[i for l in [['a', 'b', 'c'], ['d', 'e', 'f']] for i in l]
或者使用itertools.chain
作为@AshwiniChaudhary建议:
list(itertools.chain(*[['a', 'b', 'c'], ['d', 'e', 'f']]))
答案 4 :(得分:1)
尝试列表对象的“extend”方法:
>>> res = []
>>> for list_to_extend in range(0, 10), range(10, 20):
res.extend(list_to_extend)
>>> res
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
或更短:
>>> res = []
>>> map(res.extend, ([1, 2, 3], [4, 5, 6]))
>>> res
[1, 2, 3, 4, 5, 6]
答案 5 :(得分:0)
mergedlist = list_letters[0] + list_letters[1]
这假设您有一个静态长度列表,并且您总是希望合并前两个
>>> list_letters=[['a','b'],['c','d']]
>>> list_letters[0]+list_letters[1]
['a', 'b', 'c', 'd']