调用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://*.com/questions/6503388/prevent-memory-error-in-itertools-permutation

上一篇:无限生成器的Python产品


下一篇:在Python中随机选择所有组合的子集