[Python-ideas] Include partitioning in itertools

Stephen J. Turnbull stephen at xemacs.org
Sun Nov 1 20:25:21 EST 2015


Albert ten Oever writes:

 > I want to propose to include a 'partition' (mathematically more
 > correct: a set partition) function in itertools. To my knowledge,
 > partitioning of a set (iterable) is quite a common thing to do and
 > a logic extension of the combinatoric generators in itertools.

Even after other discussion, I don't understand.  Sets and iterables
are rather different things.  The recipe from ActiveState would be
perfectly happy with partitioning "aaaaaa", but that sequence is not a
reasonable representation of a set (I would normally say it has only
one element despite being repeated 6 times), and the results makes no
sense as a set, either.  For example, as sets ["aa", "aaaa"] and
["aaaa", "aa"] are the same.  And I suspect that you might get
anomolies using a sequence or iterable-based algorithm (for example,
(["a"] is not ["a"]) and ("a" is "a") both evaluate to true because of
the way the CPython implementation handles different objects).

I'm also not clear on what you would hope to get from an infinite
iterator.  I assume passing an infinite iterator would be a programmer
error, but you could also restrict the function to working on concrete
sequences.

All that doesn't mean it's a bad idea, but at this point it's
definitely underspecified.

I'd also prefer a more explicit name, such as enumerate_partitions.

Steve




More information about the Python-ideas mailing list