[Python-ideas] Add orderedset as set(iterable, *, ordered=False) and similarly for frozenset.

Ed Kellett edk141 at gmail.com
Sun Feb 8 01:37:19 CET 2015


On Sun Feb 08 2015 at 12:18:41 AM Andrew Barnert <abarnert at yahoo.com> wrote:
>
> In a+b, I'd expect that all of the elements of b will appear in the result
> in the same order they appear in b. That's how it works for sequences. But
> it can't work that way for ordered sets if there are any elements of b also
> in a.
>

Well, a + b would add elements of b to a in the order they appear in b—they
just wouldn't appear there.


> More importantly, that's the entire point of ordered sets. If you wanted
> to preserve (or didn't care about) duplicates, you'd just use a list.
>

And that's why a + b behaving that way makes sense. To me.


> What you actually want here is an "unsorted union"--all the elements of a
> in order, then all the elements of b except those also in a in order, and
> so on. (That's "unsorted" as opposed to a sorted union, where the sequences
> are interleaved in such a way as to preserve a shared ordering rule.) It
> seems a lot more natural to call that "union" than concatenate. But it
> might be better to explicitly call it "unsorted union", or even to just not
> provide it. (IIRC, in Mathematica, you do it by concatenating the sets into
> a list, then calling DeleteDuplicates to turn the result back into an
> ordered set, or by calling Tally on the concatenated list and selecting the
> ones whose tally is 1, or various other equivalents.)
>

Well, I don't really understand why it seems natural to call it union
rather than concatenate - it is appending each element in order, which is
exactly what concatenation is.

I think this might just be a fundamental difference between our mental
models of sets and union, though, so there's probably little point arguing
about it. I think "unsorted union" is reasonable.


> The fact that unordered union isn't commutative doesn't mean it's not a
> reasonable extension of union. The best-known extension of union is
> probably the disjoint union, which also isn't commutative: {1, 2} U* {0, 1}
> = {(1,0), (2,0), (0,1), (1,2)} but {0, 1} U* {1, 2} = {(0, 0), (1, 0), (1,
> 1), (2, 1)}.
>

But disjoint union has even less in common with union than unsorted union,
and I don't think anybody drops the "disjoint" and conflates it with
regular union.

Ed Kellett
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150208/fce6584e/attachment.html>


More information about the Python-ideas mailing list