2016-11-20 65 views
-1

我有一个列表如何将Python中的列表与自己结合?

L=[['g1','g2'],['g3'],['g4','g5','g6']] 

现在我想

L*L=[['g1','g2','g3'],['g1','g2','g4','g5','g6'],['g3','g4','g5','g6']] 

我怎么做在Python 3.5

+0

你可以在'g5'之后的第一个代码块中添加mssing'''吗?一个字符编辑是不允许的。 – dahrens

回答

3

你想两件事情

  • itertools.combinations(L, 2)让所有对列表中的子列表
  • 结合每对做一个新的列表

综上所述

import itertools 
LL = [a + b for a, b in itertools.combinations(L, 2)] 
+0

itertools没有工作的人...一些有点名称错误 –

+0

是的,它的工作... thanx人 –

3

嵌套你可以简单地做这些for循环

l=[[1,2],[3],[4,5,6]] 
lxl=[] 
for i in range(0,len(l)): 
    for j in range(i+1,len(l)): 
     lxl.append(l[i]+l[j]) 

1×会是这个样子 [[1, 2, 3], [1, 2, 4, 5, 6], [3, 4, 5, 6]]

+0

它的工作... thanx人 –

相关问题