【问题标题】:Making All possible combinations of given set with predetermined length [duplicate]使给定集合的所有可能组合具有预定长度[重复]
【发布时间】:2021-01-04 19:38:49
【问题描述】:

好的,我有一个清单

输入:[0,1,2,3,4,5]

输出:[0000,0001,0010,0100,1000,0002...]

我想出了一个非常愚蠢的方法,但我正在寻找更有效的方法。

def makeMoves():
    combList = []
    moves = ['0','1','2','3','4','5']
    while len(combList) < 1296:
        move = ''
        for i in range(4):
            move += random.choice(moves)
        if not move in combList:
            combList.append(move)
    return combList

提前致谢!

【问题讨论】:

  • 你应该看看itertools

标签: python


【解决方案1】:
from itertools import permutations

inp = [0,1,2,3,4,5]
result = [''.join([str(x) for x in element]) for  element in permutations(inp, 5)]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    相关资源
    最近更新 更多