python堆栈溢出的原因_如何在python中生成排列时阻止堆栈溢出

我试图在不使用itertools的情况下在

python中生成排列.到目前为止,这是我的代码:

def generatePermutations(minVal, maxVal, arrayLength, depth = -1, array = []):

if(depth == -1): # set all values to minVal initially

for i in range(arrayLength):

array.append(minVal)

depth += 1

generatePermutations(minVal, maxVal, arrayLength, depth, array) # recurse

elif depth < arrayLength:

a.append(array[:]) # a is a list declared above the function

current = 0

while current <= depth:

if array[current] < maxVal:

array[current] += 1

break

else:

if current < depth:

array[current] = minVal

else:

depth += 1

array[current] = minVal

if depth < arrayLength:

array[depth] += 1

break

current += 1

generatePermutations(minVal, maxVal, arrayLength, depth, array)

该功能适用​​于足够小的数字.例如,generatePermutations(1,2,2)使用以下内容填充列表a:

[1, 1]

[2, 1]

[1, 2]

[2, 2]

但是,当我尝试创建长度为9的数组(generatePermutations(1,9,9))的排列时,我会在函数完成之前很久就遇到堆栈溢出错误.有没有办法阻止这种情况?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值