Do iterators save memory in Python?

前端 未结 2 672
花落未央
花落未央 2021-01-12 04:21

I don\'t quite understand how iterators have memory in Python.

>>> l1 = [1, 2, 3, 4, 5, 6]
>>> l2 = [2, 3, 4, 5, 6, 7]
>>> iz = iz         


        
2条回答
  •  无人及你
    2021-01-12 05:18

    >>> l1 = [1, 2, 3, 4, 5, 6]
    >>> l2 = [2, 3, 4, 5, 6, 7]
    >>> iz = izip(l1, l2)
    

    We still require O(min(l1, l2)) memory as we need to load the lists l1 and l2 in memory.

    With zip you need storage for the two original lists plus the zipped list. With izip you don't store the zipped list.

    Big O notation isn't particularly helpful here if you have to work with a real physical machine instead of some abstract concept of a machine. There's a hidden constant multiplier on your O(n) calculations that could influence the practicality of the code well before n tends to infinity.

    >>> l1 = ( n for n in [1, 2, 3, 4, 5, 6] )
    >>> l2 = ( n for n in [2, 3, 4, 5, 6, 7] )
    >>> iz = izip(l1, l2)
    

    We need to load the lists before converting them into generators, right? This means we'll waste memory. So - what is the point of generators here as well.

    No point to generators here. Any time you see n for n in without either a more complex expression before the for or an if filter after it, that's a code smell as you could just have used the original sequence directly. The generators only become useful when you transform the input values into something else or filter them.

提交回复
热议问题