我正在编写一个打印 [a,b,c,d] 的所有排列的代码。我不想使用递归函数,而是使用了 4 个 for 循环,但我的代码的缺点是循环有与列表中的元素数量相同。我的问题是是否可以编写与元素数量无关的代码。
alphabet=["a","b","c","d"]
for first in alphabet:
for second in alphabet:
if second != first:
for third in alphabet:
if third!=second and third!=first :
for fourth in alphabet:
if fourth!=first and fourth!=second and fourth != third:
print(first,second,third,fourth)
芜湖不芜
哆啦的时光机
相关分类