调用list(itertools.combinations(keys,3))出现MemoryError的解决办法

在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

posted @ 2019-02-28 17:31  lijavasy  阅读(900)  评论(0编辑  收藏  举报