【问题标题】:spyder crash itertools.combinationsspyder 崩溃 itertools.combinations
【发布时间】:2021-04-29 05:49:55
【问题描述】:

我有这个程序:

导入迭代工具 随机导入

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'可能比较大。任何解决问题的建议将不胜感激。

【问题讨论】:

  • 不要在布尔表达式中使用&amp;。使用and。它们不一样。

标签: python spyder combinatorics


【解决方案1】:

我想你会想要使用and 而不是&amp;(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)

【讨论】:

    猜你喜欢
    • 2013-07-07
    • 2022-07-14
    • 2020-09-12
    • 2020-11-11
    • 2018-06-11
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多