将列表列表组合到python中的新列表中

时间:2013-05-21 01:49:01

标签: python nested-lists

我有一份清单

list_of_lists[['front','stuff'],['back1,'stuff'],['back3','stuff'], ...['back...', 'stuff']]

我想把前面的列表添加到每个后面的列表中:

['front','stuff','back1','stuff']
['front','stuff','back2','stuff']
['front','stuff','back3','stuff']
 ....
['front','stuff','back..','stuff']

我知道我可以设置front = lists_of_lists [0]来获取['front','stuff']

如何迭代并组合前后各自的列表?

2 个答案:

答案 0 :(得分:2)

[list_of_lists[0] + i for i in list_of_lists[1:]]  

或者如果你想避免临时切片

front = list_of_lists[0]
[front + j for i, j in enumerate(list_of_lists) if i]  

答案 1 :(得分:1)

my_lists = [['front','stuff'],['back1','stuff'],['back2','stuff']]

new_list = [my_lists[0] + sublist for sublist in my_lists[1:]]

结果:

>>> new_list
[['front', 'stuff', 'back1', 'stuff'], ['front', 'stuff', 'back3', 'stuff']]