如何在python中从允许重复的列表中获取所有可能的组合

如何在python中从允许重复的列表中获取所有可能的组合,python,python-3.x,list,combinations,itertools,Python,Python 3.x,List,Combinations,Itertools,我有一个类似于[1,2,3]的列表,我希望得到以下结果: 1 1,1 1,1,1 1,2 1,2,1 1,2,2 1,2,3 1,2 1,3 1,3,3 2,1,2 2,2,1 3,1,1 etc 我尝试过使用itertools,但我只得到了没有重复的组合 有人知道如何才能得到一个具有所需结果的列表吗?您需要itertools.compositions\u with\u replacement()并更改r。这不在您的订单中,因为不清楚这是否是一项要求,例如: In []: from itert

我有一个类似于
[1,2,3]
的列表,我希望得到以下结果:

1
1,1
1,1,1
1,2
1,2,1
1,2,2
1,2,3
1,2
1,3
1,3,3
2,1,2
2,2,1
3,1,1
etc
我尝试过使用
itertools
,但我只得到了没有重复的组合


有人知道如何才能得到一个具有所需结果的列表吗?

您需要
itertools.compositions\u with\u replacement()
并更改
r
。这不在您的订单中,因为不清楚这是否是一项要求,例如:

In []:
from itertools import combinations_with_replacement as cwr
nums = [1, 2, 3]
[x for n in range(1, len(nums)+1) for x in cwr(nums, r=n)]

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

In []:
from itertools import product
[x for n in range(1, len(nums)+1) for x in product(nums, repeat=n)]

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

您需要
itertools.compositions\u和\u replacement()
并改变
r
。这不在您的订单中,因为不清楚这是否是一项要求,例如:

In []:
from itertools import combinations_with_replacement as cwr
nums = [1, 2, 3]
[x for n in range(1, len(nums)+1) for x in cwr(nums, r=n)]

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

In []:
from itertools import product
[x for n in range(1, len(nums)+1) for x in product(nums, repeat=n)]

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

用手快速解决。如果您关心性能,您可能应该坚持使用
itertools

def all_combs(xs):
    res = []
    buf = [[]]
    lst = [[x] for x in xs]
    for _ in xs:
        buf = [r + l for r in buf for l in lst]
        res.extend(buf)

    return res

用手快速解决。如果您关心性能,您可能应该坚持使用
itertools

def all_combs(xs):
    res = []
    buf = [[]]
    lst = [[x] for x in xs]
    for _ in xs:
        buf = [r + l for r in buf for l in lst]
        res.extend(buf)

    return res

您是否尝试过
itertools.compositions\u with\u replacement()
?您是否尝试过
itertools.compositions\u with\u replacement()
?谢谢。我在提问时犯了一个错误,没有指出也需要像2,1,1这样的组合,其中第一个数字可以大于其他数字。我不知道这是否可以通过itertools实现,但Yakym提供的答案满足了我的需要。只需使用
itertools.product()
,添加。谢谢。我在提问时犯了一个错误,没有指出也需要像2,1,1这样的组合,其中第一个数字可以大于其他数字。我不知道这是否可以通过itertools实现,但Yakym提供的答案满足了我的需要。只需使用
itertools.product()
,即可。