簡體   English   中英

內核在使用 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

文檔

返回的項目數為 n! /r! / (nr)! 當 0 <= r <= n 或當 r > n 時為零。

所以不,不可能使用“普通”計算機將它們全部存儲在內存中。 在分別處理每個項目時,您必須找到解決問題的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM