nltk.LazyEnumerate
¶
-
class
nltk.
LazyEnumerate
(lst)[source]¶ 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 functionenumerate
. In particular, the following two expressions are equivalent:>>> from nltk.util 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.
Methods¶
__init__ (lst) |
|
||
count (value) |
Return the number of times this list contains value . |
||
index (value[, start, stop]) |
Return the index of the first occurrence of value in this list that is greater than or equal to start and less than stop . |
||
iterate_from (index) |
|||
unicode_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. |