Python 将序列拆分为成对组合

Python 将序列拆分为成对组合,python,list,Python,List,抱歉,如果我没有很好地阐述我的问题,我会尽力做到: 如何获得一个列表来返回其中所有可能的对组合 比如说 a = [1,2,3,4] 我想知道我怎样才能得到这样的结果: a= [ [1,2], [1,3] , [1,4], [2,3] , [2,4] , [3,4] ] 您可以在itertools模块中使用 >>> import itertools as it >>> it.combinations([1,2,3,4],2) <itertools.co

抱歉,如果我没有很好地阐述我的问题,我会尽力做到:

如何获得一个列表来返回其中所有可能的对组合

比如说

a = [1,2,3,4]
我想知道我怎样才能得到这样的结果:

a= [ [1,2], [1,3] , [1,4], [2,3] , [2,4] , [3,4] ]
您可以在itertools模块中使用

>>> import itertools as it
>>> it.combinations([1,2,3,4],2)
<itertools.combinations object at 0x106260fc8>
>>> list(it.combinations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
>>根据需要导入itertools
>>>it.组合([1,2,3,4],2)
>>>列表(it.组合([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

replicate:谢谢,我将不得不查看整个itertools模块,它看起来非常有用
>>> import itertools as it
>>> it.combinations([1,2,3,4],2)
<itertools.combinations object at 0x106260fc8>
>>> list(it.combinations([1,2,3,4],2))
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]