如何获得列表的所有组合?

The*_*gri 2 python python-itertools python-3.x

我正在尝试获取列表的所有组合。下面是一个例子:

>>> l = [1, 2, 3]
>>> combo = something
>>> print(combo)
[1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321]
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止尝试过的:

>>> import itertools
>>> numbers = [1, 2, 3]
>>> l = list(itertools.permutations(numbers))
>>> print(l)
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
Run Code Online (Sandbox Code Playgroud)

我如何获得输出[1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321]

小智 5

工作代码:

import itertools

numbers = [1, 2, 3]
result = []
for n in range(1, len(numbers) + 1):
    for x in itertools.permutations(numbers, n):  # n - length of each permutation
        result.append(int(''.join(map(str, x))))
print(result)
Run Code Online (Sandbox Code Playgroud)

输出:

[1, 2, 3, 12, 13, 21, 23, 31, 32, 123, 132, 213, 231, 312, 321]
Run Code Online (Sandbox Code Playgroud)