python - 分组列表元素

时间:2015-06-04 09:34:26

标签: python python-3.x

我想将列表中的所有元素与列表中的所有其他元素分组

代表前 -

l2 = list(zip(l1,l1[1:]))

我尝试使用zip:

l2 = [(1, 2), (2, 3)]

但它给了我:

[(1,2),(1,3),(2,3)]

期望的输出:

[1,2,3]

代表

 Uncaught Error: [$injector:modulerr] Failed to instantiate module moduleApp due to:
 Error: [$injector:modulerr] Failed to instantiate module app.module due to:
 Error: [$injector:nomod] Module 'app.module' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

1 个答案:

答案 0 :(得分:10)

它是itertools.combinations的用途:

>>> l1 = [1,2,3]
>>> from itertools import combinations
>>> list(combinations(l1,2))
[(1, 2), (1, 3), (2, 3)]