class documentation

class LazyEnumerate(LazyZip): (source)

Constructor: LazyEnumerate(lst)

View In Hierarchy

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:

>>> from nltk.collections import LazyEnumerate
>>> sequence = ['first', 'second', 'third']
>>> list(enumerate(sequence))
[(0, 'first'), (1, 'second'), (2, 'third')]
>>> list(LazyEnumerate(sequence))
[(0, 'first'), (1, 'second'), (2, 'third')]

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.

Method __init__ No summary

Inherited from LazyZip:

Method __len__ Return the number of tokens in the corpus file underlying this corpus view.
Method iterate_from Return an iterator that generates the tokens in the corpus file underlying this corpus view, starting at the token number start. If start>=len(self), then this iterator will generate no tokens.

Inherited from LazyMap (via LazyZip):

Method __getitem__ Return the i th token in the corpus file underlying this corpus view. Negative indices and spans are both supported.
Instance Variable _all_lazy Undocumented
Instance Variable _cache Undocumented
Instance Variable _cache_size Undocumented
Instance Variable _func Undocumented
Instance Variable _lists Undocumented

Inherited from AbstractLazySequence (via LazyZip, LazyMap):

Method __add__ Return a list concatenating self with other.
Method __contains__ Return true if this list contains value.
Method __eq__ Undocumented
Method __hash__ No summary
Method __iter__ Return an iterator that generates the tokens in the corpus file underlying this corpus view.
Method __lt__ Undocumented
Method __mul__ Return a list concatenating self with itself count times.
Method __ne__ Undocumented
Method __radd__ Return a list concatenating other with self.
Method __repr__ Return a string representation for this corpus view that is similar to a list's representation; but if it would be more than 60 characters long, it is truncated.
Method __rmul__ Return a list concatenating self with itself count times.
Method count Return the number of times this list contains value.
Method index Return the index of the first occurrence of value in this list that is greater than or equal to start and less than stop. Negative start and stop values are treated like negative slice bounds -- i.e., they count from the end of the list.
Constant _MAX_REPR_SIZE Undocumented
def __init__(self, lst): (source) ΒΆ
Parameters
lst:listthe underlying list