我的代码:Python的排列
a = [1,2,3]
import itertools
set(itertools.permutations(a))
我得到的输出:
{(1, 3, 2), (3, 2, 1), (1, 2, 3), (2, 3, 1), (3, 1, 2), (2, 1, 3)}
有人可以告诉我如何打印号码,如:
123
321
132
312
213
231
我的代码:Python的排列
a = [1,2,3]
import itertools
set(itertools.permutations(a))
我得到的输出:
{(1, 3, 2), (3, 2, 1), (1, 2, 3), (2, 3, 1), (3, 1, 2), (2, 1, 3)}
有人可以告诉我如何打印号码,如:
123
321
132
312
213
231
基础的在您的代码上:
import itertools
a=[1,2,3]
permutations = set(itertools.permutations(a))
for perm in permutations:
print("%s%s%s" % perm)
但是你并不需要使用set
可言,因此该解决方案(实际上是一个更好的),可就是这样过:
import itertools
a=[1,2,3]
for perm in itertools.permutations(a):
print("%s%s%s" % perm)
铸子元素为字符串,然后加入他们的行列:
for perm in permutations:
print ''.join(map(str, perm))
你尝试过什么吗? – Totem
就这么你知道,你不需要一套。 –
没有太多的“如此” - 三个项目有6个排列,所以当您打印其中的5个时,只有一个要添加。 – CiaPan