简体   繁体   English

列表回溯的python排列

[英]python permutations of list backtracking

I need to write a python program that prints in ascending order all permutation of list of numbers. 我需要编写一个Python程序,该程序以升序打印数字列表的所有排列。 I have to write two programs. 我必须编写两个程序。 One with repeat and one without repeat. 一种重复,一种不重复。 For example: list is [1,1,3] so first function output will be: 例如:list是[1,1,3],因此第一个函数的输出将是:

1,1,3
1,3,1
3,1,1

and second func output will be: 第二个func输出将是:

1,1,3
1,1,3
1,3,1
1,3,1
3,1,1
3,1,1

right now I wrote this code but I have trouble with the Asc order: 现在我写了这段代码,但是我对Asc命令有麻烦:

def perm1 (mylist, newlist):

    if len(mylist)==0:
        print ','.join(map(str,newlist))
    for i in range (0, len(mylist)):
        newlist.append(mylist.pop(i))
        perm1(mylist, newlist)
        mylist.insert(i, newlist.pop())

Any help would be appreciated 任何帮助,将不胜感激

itertools.permutations() would be perfect for this! itertools.permutations()将是完美的选择! You could use it like this: 您可以这样使用它:

from itertools import permutations

def perm1(my_list):
    return " ".join(",".join((str(si) for si in i)) for i in sorted(p(my_list))[::-1])

What permutations is is a generator. permutations是一个生成器。 This yields every single permutations, but some will be equivalent. 这将产生每个单个排列,但有些将是等效的。 It is in ascending order, as sorted gives the permutations from lowest to highest and [::-1] reverses it. 它以升序排列, sorted的排列顺序从最低到最高, [::-1]将其反转。 For descending order, remove the splice ( [::-1] ). 对于降序,请删除接头( [::-1] )。 One without duplicates can make use of sets and the fact that {0, 5, 6, 7} == {0, 7, 7, 5, 6, 0} . 没有重复项的人可以利用sets{0, 5, 6, 7} == {0, 7, 7, 5, 6, 0}的事实。

from itertools import permutations

def perm2(my_list):
    return " ".join(",".join((str(si) for si in i)) for i in reversed(sorted(set(p(my_list)))))

This shows a different way to reverse, using the reversed function, but is functionally the same. 这显示了使用reversed功能的另一种反向方式,但功能上相同。

You can make a function that takes (optional) ascending and keep_duplicates parameter will return in ascending order if ascending is True else in descending for ascending and keep_duplicates if True. 你可以做一个函数,它(可选) ascendingkeep_duplicates参数将在升序返回,如果上升是降序升序和其他真keep_duplicates如果真。

from itertools import permutations as p

def perm3(my_list, keep_duplicates=True, ascending=True):
    splice = -2 * reverse + 1 # -1 if True else 1
    if keep_duplicates:
        return " ".join(",".join((str(si) for si in i)) for i in sorted(p(my_list))[::splice])
    return " ".join(",".join((str(si) for si in i)) for i in sorted(set(p(my_list)))[::splice])

Have a look at: https://docs.python.org/2/library/itertools.html#itertools.product 看看: https : //docs.python.org/2/library/itertools.html#itertools.product

Add import: 添加导入:

from  itertools  import permutations

Repeatable permutation: 可重复排列:

aux = [1,1,3]
#Repeatable permutations
for p in sorted(permutations(aux)):
    print p

And for no repeatable: 不可重复:

aux = [1,1,3]
listunique=[]
#No repeatable permutations 
for p in sorted(permutations(aux)):
    if p not in listunique:
        listunique.append(p)

for i in listunique:
    print i

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM