Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102667

Re: Set Operations on Dicts

From Marco Kaulea <marco.kaulea@gmail.com>
Newsgroups comp.lang.python
Subject Re: Set Operations on Dicts
Date 2016-02-08 13:33 +0100
Message-ID <mailman.95.1454935075.2317.python-list@python.org> (permalink)
References <mailman.86.1454925476.2317.python-list@python.org> <lf5fux3xuov.fsf@ling.helsinki.fi>

Show all headers | View raw


On Mon, Feb 8, 2016 at 1:17 PM, Jussi Piitulainen <
jussi.piitulainen@helsinki.fi> wrote:

> I think nobody was quite willing to lay down the law on which dictionary
> would take precedence when they have keys in common but different values
> on those keys. Both ways make sense, and sometimes you want something
> like arithmetic done to combine the values on common keys.
>
I don't expect this to be really useful. I only thought about using a set
as a second argument.


But that's an interesting proposal to only allow sets as the second
> argument. Those particular cases may not be *too* difficult to express
> as comprehensions, though still quite a mouthful compared to your
> suggestion:
>
That is the restriction I had in mind.

{ k:d[k] for k in d if k in s }     # d & s
>
> { k:d[k] for k in d if k not in s } # d - s
>
> That is basically what I did. But I expect this could be quite slow, since
it has to take
each value in s and perform a lookup in d. I would expect the pure set
implementation
to be more optimized.


Also, what would be the nicest current way to express a priority union
> of dicts?
>
> { k:(d if k in d else e)[k] for k in d.keys() | e.keys() }

This seems like it might be useful for default configurations, as that is
currently
quite the hassle to do with `x = conf.get('x', fallback="default")

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Set Operations on Dicts Marco Kaulea <marco.kaulea@gmail.com> - 2016-02-08 10:56 +0100
  Re: Set Operations on Dicts Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-02-08 14:17 +0200
    Re: Set Operations on Dicts Marco Kaulea <marco.kaulea@gmail.com> - 2016-02-08 13:33 +0100
    Re: Set Operations on Dicts Matt Wheeler <m@funkyhat.org> - 2016-02-08 13:32 +0000
      Re: Set Operations on Dicts Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-02-08 16:02 +0200
    Re: Set Operations on Dicts Random832 <random832@fastmail.com> - 2016-02-08 09:52 -0500
      Re: Set Operations on Dicts Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-02-08 17:05 +0200
  Re: Set Operations on Dicts Grobu <snailcoder@retrosite.invalid> - 2016-02-08 13:47 +0100
    Re: Set Operations on Dicts Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-08 09:12 -0700
      Re: Set Operations on Dicts Grobu <snailcoder@retrosite.invalid> - 2016-02-09 08:21 +0100
        Re: Set Operations on Dicts Ian Kelly <ian.g.kelly@gmail.com> - 2016-02-09 08:14 -0700

csiph-web