我从一组数字生成所有组合,然后想要生成这些组合的组合。由于大量可能的组合,我不断收到内存错误。我看了下面的问题,但没有一个真正解决了我的问题:
Creating all combinations of a set and running out of memory
Python itertools.combinations() memory problems
Python list memory error
我使用以下方法生成我的列表:
#generate all combinations of 1 and 0 of size 30
set_1 = itertools.product([0,1], repeat = 30)
#generate all combinations of set 1, of size 5
set_2 = [tuple(c) for c in pulp.allcombinations(set_1, 5)]
for sets in set_2:
print(sets)
生成set_2时发生内存错误。我仍然希望能够迭代set_2,因为稍后我将需要访问这些集合。我曾考虑将这些集合写入txt文件,但我想将其保存为最后的手段。