gpt4 book ai didi

Python Reduce 条件表达式

转载 作者:太空宇宙 更新时间:2023-11-04 10:26:45 24 4
gpt4 key购买 nike

我有 9 个变量 a、b、c、d、e、f、g、h、i,我将它们循环到 9 for 循环中,从 0 到 9。但范围可能会有所不同。

我想要它们的所有序列 abcdefghi,这样就没有重复的数字。

现在我有这个,如下:

for a in range(0, 9): 
for b in range(0,9): #it doesn't have to start from 0
....
for i in range(0, 9):
if a != b and a != c ... a != i
b != c and b != d ... b != i
c != d and c != e ... c != i
... h != i:

print (a,b,c,d,e,f,g,h,i)

有9个! = 362880 个,

但是我怎样才能减少条件表达式呢?如果 for 循环的范围不同怎么办?

提前致谢!

最佳答案

您可以使用 itertools 模块简单地执行此操作:

from itertools import permutations

for arrangement in permutations('abcdefghi', 9):
print ''.join(arrangement)

关于Python Reduce 条件表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28932722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com