Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表的无重复排列_Python_Permutation_Itertools - Fatal编程技术网

Python 列表的无重复排列

Python 列表的无重复排列,python,permutation,itertools,Python,Permutation,Itertools,itertools中的命令置换 permutations([0,1,1]) 返回 (0, 1, 1), (0, 1, 1), (1, 0, 1), (1, 1, 0), (1, 0, 1), (1, 1, 0) 有办法回去吗 (0,1,1), (1,0,1), (1,1,0) 也就是说,对于任意整数列表,如果原始列表中的元素重复,则获取所有置换,但没有重复的元素?您可以将返回值强制转换为一个集合: print(list(set(permutations([0,1,1])))) 输出:

itertools中的命令置换

permutations([0,1,1]) 
返回

(0, 1, 1), (0, 1, 1), (1, 0, 1), (1, 1, 0), (1, 0, 1), (1, 1, 0)
有办法回去吗

(0,1,1), (1,0,1), (1,1,0)

也就是说,对于任意整数列表,如果原始列表中的元素重复,则获取所有置换,但没有重复的元素?

您可以将返回值强制转换为一个集合:

print(list(set(permutations([0,1,1]))))
输出:

[(0, 1, 1), (1, 1, 0), (1, 0, 1)]

元素总是可散列的吗?Willem的意思是
set(排列([0,1,1]))
这确实是一种幼稚的方法。问题是,可区分排列的数量可能远小于排列的总数,因此这通常是非常低效的。当然,这完全取决于问题的大小。对于某些用例来说,做更多的事情是没有意义的,所以它可能需要+1