1. 程式人生 > >調用list(itertools.combinations(keys,3))出現MemoryError的解決辦法

調用list(itertools.combinations(keys,3))出現MemoryError的解決辦法

res better span https pre ren ons not per

在python3.6中,如下例子:

keys = list(newKeywords.keys())

resultkeys = list(itertools.combinations(keys,3))

當上面中的Keys太長時會發生MemoryError錯誤,解決辦法:

keys = list(newKeywords.keys())

resultkeys = itertools.combinations(keys,3)

for rkey in resultkeys:
  print(rkey)

據下面鏈接中所說,只有當前用到的組合才存放在內存中。

原文如下:

by doing this, python will keep in memory only the currently used permutation, not all the permutations (in term of memory usage, it is really better ;) )

reference:https://stackoverflow.com/questions/6503388/prevent-memory-error-in-itertools-permutation

調用list(itertools.combinations(keys,3))出現MemoryError的解決辦法