Skip to content Skip to sidebar Skip to footer

Itertools Combination Memory Error With Python

I have a Memory error when I use combinations with a big list of list like len 735. Any way to do a similar process but without the error? from itertools import combinations valor

Solution 1:

If your list has 735 elements, then there are (735 choose 3) = 65907695 combinations of three elements. There is probably no need to keep all of these 3-tuples in memory at the same time, so don't build a list out of them; just iterate directly.

for c in combinations(valores, 3):
    # do something with c

Solution 2:

You can try to prevent memory error.

comb_iter = itertools.combinations(valores,3)

for item in comb_iter:
   do_the_stuff(item)

As a result, python will keep in memory only the currently used combination, not all the combinations.

Post a Comment for "Itertools Combination Memory Error With Python"