内核在使用itertools.combinations时死亡

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

我正在使用itertools.combinations生成列表元素的所有可能组合。但是内核总是在2分钟后死亡。我认为这是一个内存问题。还有其他有效的方法来产生所有可能的组合并将其存储在某些数据结构中吗?

total = ['E', 'ENE', 'ESE', 'N', 'NNE', 'NNW', 'NW', 'S', 'SE', 'SSE',
       'SSW', 'SW', 'W', 'WNW', 'WSW', 'station_0', 'station_1',
       'station_2', 'station_3', 'station_4', 'station_5', 'station_6',
       'station_7', 'station_8', 'station_9', 'station_10', 'year',
       'month', 'day', 'hour', 'SO2', 'NO2', 'CO', 'O3', 'TEMP', 'PRES',
       'DEWP', 'RAIN', 'WSPM']            

total_combinations = []           

for i in range(2,len(total)+1):
    current_comb = list(combinations(total,i))
    total_combinations = total_combinations + current_comb

python kernel combinations itertools
1个回答
1
投票

如果我的数学正确,则您有549755813848个组合!

>>> from math import factorial
>>> n = len(total)
>>> sum(factorial(n) / factorial(r) / factorial(n-r) for r in range(2,n+1))
549755813848.0

docs

返回的项目数为n! / r! /(n-r)!当0 <= r <= n或当r> n时为零。

因此,不可能将它们全部存储在任何“普通”计算机上的内存中。您必须找到一种方法来解决您的问题,同时分别处理每个项目。

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