在26个大写英文字母中,取N个字母进行排列,输出所有排列的可能,N由用户给定。
例如,N若等于3,则会有26×25×24中可能性,程序输出需要罗列每一种可能性ABC,XYZ,ABZ,等等。
在26个大写英文字母中,取N个字母进行排列,输出所有排列的可能,N由用户给定。
例如,N若等于3,则会有26×25×24中可能性,程序输出需要罗列每一种可能性ABC,XYZ,ABZ,等等。
承邀,楼主的意思应该是全组合,按照递归思想可建立如下程序:
def alphabet(iterable, r):
pool = list(map(str, iterable))
n = len(pool)
if r > n:
return
indices = list(range(r))
yield list(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i + 1, r):
indices[j] = indices[j - 1] + 1
yield list(pool[i] for i in indices)
alphabet_list=['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
print(list(alphabet(alphabet_list, 3))) #N可以自己设置,这里输入3
结果如下:
望采纳!