简体   繁体   English

当给定一个返回列表的下一个排列的函数时,查找列表的所有排列

[英]Finding All permutations of a list when given a function that returns the next permutation of a list

In my assignment this week I was asked to write a python script that takes a number n and returns all permutations of [0,1,2,...,n-1].在本周的作业中,我被要求编写一个 Python 脚本,该脚本采用数字 n 并返回 [0,1,2,...,n-1] 的所有排列。 So far I have written a script that takes a list and returns the next permutation of the list.到目前为止,我已经编写了一个脚本,它接受一个列表并返回列表的下一个排列。 I am looking for ideas on how I can write the script based on what I've written so far.我正在寻找关于如何根据我目前所写的内容编写脚本的想法。

def next_permutation(p):
    a = len(p)
    i = a -2 
    while i >= 0 and p[i] >= p[i+1]:
        i = i-1
    if i == -1:
        return []

    j = i+1
    while j < a and p[j] >= p[i]:
        j += 1
    j-=1

    p[i], p[j] = p[j], p[i]

    k = i + 1
    l = a - 1

    while k < l:
        p[k], p[l] = p[l], p[k]
        k += 1
        l -= 1
    return p

EDIT: this is the code that returns the next permutation of a list.编辑:这是返回列表下一个排列的代码。 I wrote this entirely based on the instruction provided by my instructor.我完全根据导师提供的说明写了这篇文章。

Since you want to have all the permutations of a list with numbers from 0 to n-1, you already have clear steps that you need to take:由于您希望拥有从 0 到 n-1 的数字的列表的所有排列,因此您已经有了需要采取的明确步骤:

  1. Create a list that contains all numbers from 0 to n-1:创建一个包含从 0 到 n-1 的所有数字的列表:

This can be easily done with the in-built range() function since it is mostly used for exactly that purpose:这可以通过内置的range()函数轻松完成,因为它主要用于该目的:

range(stop)范围(停止)

This is a versatile function to create iterables yielding arithmetic progressions.这是一个多功能函数,用于创建产生算术级数的迭代。

  1. Calculate the amount of permutations that such list would have:计算此类列表将具有的排列数量:

Math tells us that having N elements, there will be N!数学告诉我们,有 N 个元素,就会有 N! different permutations of those elements, wher !这些元素的不同排列,哪里 means factorial.表示阶乘。 We can import factorial function from math module which would quickly allow us to calculate the amount of permutations your list will have:我们可以从 math 模块导入阶乘函数,这将允许我们快速计算您的列表将具有的排列数量:

from math import factorial
print(factorial(4)) # 24
  1. Call your function next_permutation(p) that you already wrote that many times and yield each and every permutation.调用您已经多次编写的函数next_permutation(p)并产生每个排列。

To return something more than once from a function, you can use yield insted.要从函数中多次返回某些内容,您可以使用yield insted。

With these steps in mind, you can create something similar to this:记住这些步骤后,您可以创建类似于以下内容的内容:

def all_permutations(n):

    # Constructing a list that contains all numbers from 0 to n-1
    integer_list = list(range(n))

    # Calculating the amount of permutations such list would have
    permutation_count = factorial(n)

    # Output that many permutations
    for _ in range(permutation_count):
        yield integer_list
        integer_list = next_permutation(integer_list)

This generator function will yield all permutations of a list containing numbers from 0 to n-1 which is exactly what you need.此生成器函数将生成包含从 0 到 n-1 的数字的列表的所有排列,这正是您所需要的。

To create a list that would contain all of the permutations you can write something simple like:要创建一个包含所有排列的列表,您可以编写一些简单的内容,例如:

n = 4
all_permutations = list(all_permutations(n))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定分组元素列表时,查找 hash 的所有可能排列 - Finding all possible permutations of a hash when given list of grouped elements 给定一个数字列表,找出加到 100 的特定长度的所有排列 - Given a list of numbers find all the permutations that add to 100 of a certain length 关于尝试打印给定列表的所有排列的问题 - Question about an attempt to print all permutations of a given list 递归 function 生成排列正确打印每个排列但不能将其添加到列表中 - Recursive function to generate permutations correctly prints each permutation but can not add it to a list 在 python 中查找给定字符串的所有可能排列 - Finding all possible permutations of a given string in python 给定每个数字或索引的选择列表,是否有生成可能排列的函数? - Is there a function to generate possible permutations given list of choices for each digit or index? 在Python中生成列表的所有排列时,列表中元素的最大数量 - The maximum number of element in a list when generate all the permutations of a list in Python 给定一个不同单词的列表,实现一个 function 返回包含所有元音的所有单词的列表(不区分大小写) - Given a list of different words, implement a function that returns the list of all words that contain all vowels (case insensitive) 打印列表中所有字符串的排列 - print permutations of all strings in a list 列表的相邻元素中的所有排列 - All permutations in adjacent elements of list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM