1 | initial version |
In your code, by replacing []
by ()
and range()
by xrange()
, you build a generator, that will loop in real time, without storing all values in a list:
sage: A = ((a,b,c,d) for a in xrange(100) for b in xrange (100) for c in xrange (100) for d in xrange (100))
sage: A
<generator object <genexpr> at 0x5d269b0>
sage: for f in A:
....: print f
(0, 0, 0, 0)
(0, 0, 0, 1)
(0, 0, 0, 2)
(0, 0, 0, 3)
(0, 0, 0, 4)
...
2 | No.2 Revision |
In your code, by just replacing []
by ()
and range()
by xrange()
, you build a generator, that will loop in real time, without storing all values in a list:list, saving a lot of memory:
sage: A = ((a,b,c,d) for a in xrange(100) for b in xrange (100) for c in xrange (100) for d in xrange (100))
sage: A
<generator object <genexpr> at 0x5d269b0>
sage: for f in A:
....: print f
(0, 0, 0, 0)
(0, 0, 0, 1)
(0, 0, 0, 2)
(0, 0, 0, 3)
(0, 0, 0, 4)
...
By the way, if all prescribed values are the same, you can also do:
sage: B = cartesian_product_iterator([xrange(100) for i in range(4)])
sage: for f in B:
....: print f
(0, 0, 0, 0)
(0, 0, 0, 1)
(0, 0, 0, 2)
(0, 0, 0, 3)
(0, 0, 0, 4)
...