Re: Set Operations on Dicts

2016-02-09 Thread Ian Kelly
On Tue, Feb 9, 2016 at 12:21 AM, Grobu wrote: > On 08/02/16 17:12, Ian Kelly wrote: > >> dict does already expose set-like views. How about: >> >> {k: d[k] for k in d.keys() & s} # d & s >> {k: d[k] for k in d.keys() - s} # d - s >> > Interesting. But seemingly only applies to Python 3. Substit

Re: Set Operations on Dicts

2016-02-08 Thread Grobu
On 08/02/16 17:12, Ian Kelly wrote: dict does already expose set-like views. How about: {k: d[k] for k in d.keys() & s} # d & s {k: d[k] for k in d.keys() - s} # d - s Interesting. But seemingly only applies to Python 3. -- https://mail.python.org/mailman/listinfo/python-list

Re: Set Operations on Dicts

2016-02-08 Thread Ben Finney
Marco Kaulea writes: > In one talk (I think it was [1]) it was described that sets are basically > dicts without the values. It seems an unhelpful thing to say about ‘set’, I disagree with that characterisation. > Therefor it should be easy to apply set operations on dicts Yes, t

Re: Set Operations on Dicts

2016-02-08 Thread Ian Kelly
On Mon, Feb 8, 2016 at 5:47 AM, Grobu wrote: > You can use dictionary comprehension : > > Say : > dict1 = {'a': 123, 'b': 456} > set1 = {'a'} > > intersection : { key:dict1[key] for key in dict1 if key in set1 } > {'a': 123} > > difference : { key:dict1[key] for key in dict1 if not key i

Re: Set Operations on Dicts

2016-02-08 Thread Jussi Piitulainen
Random832 writes: > On Mon, Feb 8, 2016, at 08:32, Matt Wheeler wrote: >> On 8 February 2016 at 12:17, Jussi Piitulainen wrote: >> > 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() } >> >> Sin

Re: Set Operations on Dicts

2016-02-08 Thread Random832
On Mon, Feb 8, 2016, at 08:32, Matt Wheeler wrote: > On 8 February 2016 at 12:17, Jussi Piitulainen > wrote: > > 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() } > > Since Python 3.5: {**e, **d}

Re: Set Operations on Dicts

2016-02-08 Thread Jussi Piitulainen
Matt Wheeler writes: > On 8 February 2016 at 12:17, Jussi Piitulainen wrote: >> 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() } > > Since Python 3.5: {**e, **d} Thanks. I have considered this news

Re: Set Operations on Dicts

2016-02-08 Thread Matt Wheeler
On 8 February 2016 at 12:17, Jussi Piitulainen wrote: > 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() } Since Python 3.5: {**e, **d} -- Matt Wheeler http://funkyh.at -- https://mail.python.org/ma

Re: Set Operations on Dicts

2016-02-08 Thread Grobu
You can use dictionary comprehension : Say : dict1 = {'a': 123, 'b': 456} set1 = {'a'} intersection : >>> { key:dict1[key] for key in dict1 if key in set1 } {'a': 123} difference : >>> { key:dict1[key] for key in dict1 if not key in set1 } {'b': 456} -- https://mail.python.org/mailman/listinfo

Re: Set Operations on Dicts

2016-02-08 Thread Marco Kaulea
On Mon, Feb 8, 2016 at 1:17 PM, Jussi Piitulainen < jussi.piitulai...@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 w

Re: Set Operations on Dicts

2016-02-08 Thread Jussi Piitulainen
Marco Kaulea writes: > In one talk (I think it was [1]) it was described that sets are > basically dicts without the values. Therefor it should be easy to > apply set operations on dicts, for example: > > {'a': 123, 'b': 456} & {'a'} => {&

Set Operations on Dicts

2016-02-08 Thread Marco Kaulea
Hi, In one talk (I think it was [1]) it was described that sets are basically dicts without the values. Therefor it should be easy to apply set operations on dicts, for example: {'a': 123, 'b': 456} & {'a'} => {'a': 123} {'a': 12