超过python排列时间限制

问题描述 投票:0回答:1

我有一个n,直到1_000_000为止都有最大值

我必须用li=range(0,n)创建一个列表并找到2个项目的排列

我正在使用此代码

from itertools import permutations 
perm = permutations(li,2)
for i in list(perm): 
    print(i)  

此代码适用于较小的n

但是如果我的数量过多,我将超过时限错误

如何不使用外部库而解决这些超出超时的错误

python-3.x list permutation
1个回答
0
投票

不要强制转换以列出您的perm变量,只需在您的for循环中使用您的perm

© www.soinside.com 2019 - 2024. All rights reserved.