s1 = 'ABC'
s2 = '123'
s3 = 'xyz'
result = ''
for a in s1:
for b in s2:
for c in s3:
print(a+b+c)
之后要怎么继续排列,才能得到全排列?
如果继续print(a+c+b)把六种方式都列出来感觉有点赘余,假如要列的不止三个字符长度而是n个,要怎么写简便的代码?
# Enter a code
s1 = 'ABC'
s2 = '123'
s3 = 'xyz'
for i in s1:
for j in s2:
for k in s3:
print(i + j + k)
print(i + k + j)
print(j + k + i)
print(j + i + k)
print(k + j + i)
print(k + i + j)
# Enter a code
#coding=UTF-8
import itertools
s1='ABC'
s2='123'
s3='xyz'
count = 0
for x in s1:
for y in s2:
for z in s3:
for i in itertools.permutations([x,y,z],3):
print(i)
酱?虽然很弱智但是好歹是个办法
如果按你的思路的话 我想不出
如果实现目的的话 你可以试试这样 看看对不对
s1 = 'ABC' s2 = '123' s3 = 'xyz' s = s1 + s2 + s3 count = 0 for a in s: for b in s: for c in s: if (a != b and b != c and a != c ): print(a+b+c) count+=1 print(count) print(9*8*7)
A1x
A1y
A1z
A2x
A2y
A2z
A3x
A3y
A3z
B1x
B1y
B1z
B2x
B2y
B2z
B3x
B3y
B3z
C1x
C1y
C1z
C2x
C2y
C2z
C3x
C3y
C3z
这个不行吗
你想要什么样的全排列