嵌套循环求排列
#双重
target = 'abc'
for x in target:
for y in target:
if x != y:
print(x, y)
#多重
target = 'abc'
for x in target:
for y in target:
for z in target:
if x!=y and x!=z and y!=z:
print(x, y, z)
#递归嵌套
def abc_arrange(n, data):
# 遍历abc
for x in data:
print(x)
# 调用自己2次
for i in range(n):
abc_arrange(n-1, data) # 传入的参数为n-1为嵌套循环
n = 2 # 选2个进行组合
target = 'abc' # 组合为abc
abc_arrange(n, target)
def abc_arrange(n, data):
if n == 0:
return
# 遍历abc
for x in data:
print(x)
abc_arrange(n-1, data)
n = 2 # 选2个进行组合
target = 'abc' # 组合为abc
abc_arrange(n, target)