0

我有一个列表列表

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']

我如何迭代和组合正面和背面,每个到他们自己的列表中?

4

2 回答 2

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]  
于 2013-05-21T01:53:38.933 回答
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']]
于 2013-05-21T01:54:14.833 回答