蜘蛛崩溃 itertools.combinations

spyder crash itertools.combinations

我有这个程序:

导入 itertools 随机导入

randomlist = []
for i in range(0,32):
    a = random.randint(1,30)
    randomlist.append(a)
    print(randomlist)

a = randomlist
all_combinations = []
min_num_of_funds = 4 
max_num_of_funds = 10
for i in range(0,len(a)+1):
    if i>=min_num_of_funds & i<=max_num_of_funds:
        comb = list(itertools.combinations(a,i))
        all_combinations.append(comb)
        

我收到以下错误

内核已死,正在重新启动

Restarting kernel... 


 Populating the interactive namespace from numpy and matplotlib

[SpyderKernelApp] WARNING | No such comm: 4b233da0a85511ebb62bacde48001122
[SpyderKernelApp] WARNING | No such comm: 48c21daea86b11ebb62bacde48001122
[SpyderKernelApp] WARNING | No such comm: 61748e76a86c11ebb62bacde48001122
[SpyderKernelApp] WARNING | No such comm: fd9fd78ca86d11ebb62bacde48001122


Kernel died, restarting

我的感觉是问题的根源是列表'a'可能比较大。任何解决问题的建议都将不胜感激。

我想你会想要使用 and 而不是 &(python 中的按位和运算符)。

您 运行 内存不足,因为 i 的大值仍将计算为 True:

i = 100

i >= 4 & i <= 10
Out[33]: True
sys.getsizeof(list(itertools.combinations(list(range(32)), 10)))
Out[29]: 572759960 (~572 MB)

那已经在内存中了,但是由于那个错误,你的程序正在计算更大的 i 值,这导致 Spyder 运行 内存不足。

提出以下建议(这对我有用,不会 运行内存不足):

randomlist = []
for i in range(0,32):
    a = random.randint(1,30)
    randomlist.append(a)
    print(randomlist)

a = randomlist
all_combinations = []
min_num_of_funds = 4 
max_num_of_funds = 10
for i in range(min_num_of_funds, max_num_of_funds+1):
    comb = list(itertools.combinations(a,i))
    all_combinations.append(comb)