I would like to use itertools.chain
for efficient concatenation of lists (memoization), but I need to be able to read (or map
, etc.) the result multiple times. This example illustrates the problem:
import itertools
a = itertools.chain([1, 2], [3, 4])
print list(a) # => [1, 2, 3, 4]
print list(a) # => []
What is the best way to avoid this problem?
One such itertools function is chain (). It is a function that takes a series of iterables and returns one iterable. It groups all the iterables together and produces a single iterable as output. Its output cannot be used directly and thus explicitly converted into iterables. This function come under the category iterators terminating iterators.
They make iterating through the iterables like lists and strings very easily. One such itertools function is chain(). Note: For more information, refer to Python Itertools chain() function. It is a function that takes a series of iterables and returns one iterable. It groups all the iterables together and produces a single iterable as output.
import itertools list (itertools.islice (count (), 5)) # Output # [0, 1, 2, 3, 4] izip () returns an iterator that combines the elements of the passed iterators into tuples. It works similarly to zip (), but returns an iterator instead of a list.
You do not need any new itertools functions to write this function. See what you can come up with on your own before reading ahead. You start by creating a list of hand_size references to an iterator over deck.
As with all generators, you'll need to convert it to a list and store that result instead:
a = list(a)
This is a fundamental principle of generators, they are expected to produce their sequence only once.
Moreover, you cannot simply store a generator for memoization purposes, as the underlying lists could change. In almost all memoization use-cases, you should store the list instead; a generator is usually only a means of efficiently transforming or filtering the underlying sequences, and does not represent the data you want to memoize itself. It's as if you are storing a function, not it's output. In your specific case, if all what you are doing is using chain()
to concatenate existing lists, store those lists directly instead.
Note that this enables generators to produce endless sequences, so be careful with that you convert to a list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With