[Python-ideas] Add 'interleave' function to itertools?
Oscar Benjamin
oscar.j.benjamin at gmail.com
Fri Aug 9 18:32:13 CEST 2013
On 8 August 2013 19:48, Tim Peters <tim.peters at gmail.com> wrote:
>
> To the non-expert, which itertools-like functionalities are "core" -
> which should be derived from which others - is a mystery. So I'm with
> David on throwing them all in, "at least those that have intuitive and
> obvious names". Let a thousand redundancies bloom ;-)
They should at least go in a different module. Currently each of those
recipes is already available in more-itertools (including the proposed
first()):
http://pythonhosted.org/more-itertools/api.html
Also if they're going to become anything more than recipes then they
need to be looked at more carefully. I don't really think many of
these belong in the stdlib.
For example why not just use generators? I would find generators
easier to understand than some of these recipes e.g.:
sum(1 for x in data if x > threshold)
or
sum(x > threshold for x in data)
are easier to understand than
quantify(data, lambda x: x > threshold)
even if you're using a named function.
I think that consume() should be included in itertools. flatten() is a
synonym for chain.from_iterable and there has been some discussion
that this could go into builtins (the disagreement only seemed to be
about the name). I want take() to have different semantics than it
does i.e.:
def take(iterable, n):
chunk = list(islice(iterable, n))
if len(chunk) != n:
raise ValueError('%n items requested but %n found' % (n,
len(chunk)))
return chunk
Similarly grouper() probably doesn't do what anyone really wants
(since it discards the end of the iterable). It should really be:
def grouper(iterable, chunksize):
islices = map(islice, repeat(iter(iterable)), repeat(chunksize))
return takewhile(bool, map(list, islices))
Some recipes are just unnecessary such as random_product() which is
really just a long way to write something like:
random_pair = (random.choice(s1), random.choice(s2))
Same goes for random_permutation, random_combination, and
random_combination_with_replacement: if you're just going to build a
collection out of each iterable then what does this have to do with
iterators?
The partition() recipe is only useful if you can think of a clever way
of using it. How would you use it to do anything that would be better
than just building a list (since that's what tee() will do internally
if you consume either of the iterators)?
I use itertools all the time but really the only recipes I would use
in my own code are consume() (with the second argument made optional),
pairwise() and powerset().
Oscar
More information about the Python-ideas
mailing list