使用 itertools.combinations 时内核死机
Kernel died while using itertools.combinations
我正在使用 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
如果我的数学是正确的,你有 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:
The number of items returned is n! / r! / (n-r)! when 0 <= r <= n or
zero when r > n.
所以不,使用 'normal' 计算机不可能将它们全部存储在内存中。您必须在分别处理每个项目的同时找到解决问题的方法。
我正在使用 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
如果我的数学是正确的,你有 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:
The number of items returned is n! / r! / (n-r)! when 0 <= r <= n or zero when r > n.
所以不,使用 'normal' 计算机不可能将它们全部存储在内存中。您必须在分别处理每个项目的同时找到解决问题的方法。