Package nltk :: Module util :: Class LazyEnumerate
[hide private]
[frames] | no frames]

Class LazyEnumerate

source code

          object --+            
                   |            
AbstractLazySequence --+        
                       |        
                 LazyMap --+    
                           |    
                     LazyZip --+
                               |
                              LazyEnumerate

A lazy sequence whose elements are tuples, each ontaining a count (from zero) and a value yielded by underlying sequence. LazyEnumerate is useful for obtaining an indexed list. The tuples are constructed lazily -- i.e., when you read a value from the list, LazyEnumerate will calculate that value by forming a tuple from the count of the i-th element and the i-th element of the underlying sequence.

LazyEnumerate is essentially a lazy version of the Python primitive function enumerate. In particular, the following two expressions are equivalent:

>>> enumerate(sequence)
>>> list(LazyEnumerate(sequence))

Lazy enumerations can be useful for conserving memory in cases where the argument sequences are particularly long.

A typical example of a use case for this class is obtaining an indexed list for a long sequence of values. By constructing tuples lazily and avoiding the creation of an additional long sequence, memory usage can be significantly reduced.

Instance Methods [hide private]
 
__init__(self, lst) source code

Inherited from LazyZip: __len__, iterate_from

Inherited from LazyMap: __getitem__

Inherited from AbstractLazySequence: __add__, __cmp__, __contains__, __hash__, __iter__, __mul__, __radd__, __repr__, __rmul__, count, index

Inherited from object: __delattr__, __getattribute__, __new__, __reduce__, __reduce_ex__, __setattr__, __str__

Class Variables [hide private]

Inherited from AbstractLazySequence (private): _MAX_REPR_SIZE

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, lst)
(Constructor)

source code 
Parameters:
  • lst (list) - the underlying list
Overrides: LazyZip.__init__