【问题标题】:How can I get the permutations for every number within range 1 to lst?如何获得 1 到 lst 范围内每个数字的排列?
【发布时间】:2021-12-11 10:44:36
【问题描述】:

所以我编写了这段代码,试图返回范围 (1, lst) 内数字的所有排列。

def permutation(lst):
    if isinstance(lst, int):
        lst = list(range(1, lst + 1))
    if len(lst) == 0:
        return []
    if len(lst) == 1:
        return ({(1,)})
    if len(lst) == 2:
        return ({(1,2),(2,1)})
    l = []
    for i in range(len(lst)):
        m = lst[i]
        remLst = lst[:i] + lst[i + 1:]
        for p in permutation(remLst):
            l.append(tuple([m] + list(p)))
    return set(l)

但是,我得到的输出似乎不正确..因为当我输入时

permutation(3)

我明白了……

{(1, 2, 1), (1, 1, 2), (3, 2, 1), (2, 1, 2), (2, 2, 1), (3, 1, 2)}

但我应该得到

{(1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1)}

当我的输入是permutation(4) 我的输出是

{(4, 1, 2, 1), (1, 4, 1, 2), (4, 1, 1, 2), (3, 1, 2, 1), (4, 3, 2 , 1), (3, 2, 2, 1), (4, 3, 1, 2), (3, 4, 2, 1), (3, 2, 1, 2), (4, 2, 2 , 1), (3, 1, 1, 2), (3, 4, 1, 2), (2, 3, 2, 1), (4, 2, 1, 2), (2, 4, 1 , 2), (2, 4, 2, 1), (2, 3, 1, 2), (1, 2, 1, 2), (1, 2, 2, 1), (1, 3, 2 , 1), (2, 1, 1, 2), (2, 1, 2, 1), (1, 4, 2, 1), (1, 3, 1, 2)}

但我应该得到这个......

{(4, 3, 1, 2), (3,4, 1, 2), (3, 1, 4, 2), (3, 1, 2,4), (4, 1, 3, 2), (1, 4, 3, 2), (1, 3, 4, 2), (1, 3, 2, 4), (4, 1, 2, 3), (1, 4, 2, 3), (1, 2, 4, 3), (1, 2, 3, 4), (4, 3, 2, 1), (3, 4, 2, 1), (3, 2, 4, 1), (3, 2, 1, 4), (4, 2, 3, 1), (2, 4, 3, 1), (2, 3, 4, 1), (2, 3, 1, 4), (4, 2, 1, 3), (2, 4, 1, 3), (2, 1, 4, 3), (2, 1, 3, 4)}

我应该进行哪些更改以使我的代码返回正确的输出??

【问题讨论】:

  • 两者都一样吧?您希望输出顺序相同吗?
  • @ShashikamalRC 不是这样。看看我的排列输出(2)和预期输出
  • @ShashikamalRC 我的意思是排列(3)
  • 在代码中 lst 应该是一个列表,但你使用 3 作为参数。当你使用 3 时,lst = [1, 2, 3] ?
  • @Melo 是的,这就是我想要的。它是一个从 1 到“lst”数字的列表

标签: python function numbers set permutation


【解决方案1】:

您可以使用itertools.permutations:它完全符合您的要求。

from itertools import permutations

lst = 3
p = permutations(range(lst))
print(p)

请参阅文档here

【讨论】:

  • 感谢您的回复。但是,我试图不在输出中使用内置函数,如导入、集合或 itertools。有没有更简单的方法……比如我的代码有什么可以改变的吗?
  • 我明白了。问题应该是关于实现,而不是关于获得排列。我不知道你是否可以改写和编辑它。
【解决方案2】:

我认为你做不到

if len(lst) == 1:
    return ({(1,)})
if len(lst) == 2:
    return ({(1,2),(2,1)})

如果列表的长度为 1,并不一定意味着它的所有排列都是 1。我认为这会扰乱您的递归,请尝试以下操作:

if len(lst) == 1:
    return ({(lst[0],)})
if len(lst) == 2:
    return ({(lst[0],lst[1]),(lst[1],lst[0])})

完整代码变为:

def permutation(lst):
    if isinstance(lst, int):
        lst = list(range(1, lst + 1))
    if len(lst) == 0:
        return []
    if len(lst) == 1:
        return ({(lst[0],)})
    if len(lst) == 2:
        return ({(lst[0],lst[1]),(lst[1],lst[0])})
    l = []
    for i in range(len(lst)):
        m = lst[i]
        remLst = lst[:i] + lst[i + 1:]
        for p in permutation(remLst):
            l.append(tuple([m] + list(p)))
    return set(l)

【讨论】:

    【解决方案3】:

    这是permutations 函数的另一种实现:

    def permutations(lst):
        if len(lst) <= 1:
            return [lst]
    
        results = []
    
        for idx, item in enumerate(lst):
            sub_perms = permutations(lst[:idx] + lst[idx+1:])
            results.extend([item] + sub_perm for sub_perm in sub_perms)
    
        return results
    

    permutations([1,2,3]) 的输出:

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

    【讨论】:

      【解决方案4】:

      您可以使用简单的列表推导,这可能更符合 Python 风格。

      def permutation(M):
          def permutation_set(S):
              if not S:
                  return [[]]
              else:
                  return [[x] + p for x in S for p in permutation_set(S - {x})] 
          return permutation_set(set(range(1, M+1)))
      
      permutation(3)
      # ==> [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
      

      这段代码的解释是:

      • 给定一个数字 M,组成一个集合 S = {1, 2, ..., M} -- set(range(1, M+1))
      • 对于集合S 中的每个项目x,递归生成子集S - {x} 的排列
      • x 连接到子集的每个排列的前面 -- [x] + p for p in permutation_set(S - {x})

      对于S 中的每个x,这会产生以x 开头的S 的排列序列。

      遍历所有x 给出S 的所有排列 -- [x] + p for x in S

      【讨论】:

        猜你喜欢
        • 2012-09-23
        • 2011-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-28
        • 1970-01-01
        相关资源
        最近更新 更多