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


Groups > comp.lang.python > #67054 > unrolled thread

intersection, union, difference, symmetric difference for dictionaries

Started bymauro <mauro@gmail.com>
First post2014-02-25 20:32 +0000
Last post2014-03-15 17:24 +0000
Articles 8 on this page of 28 — 17 participants

Back to article view | Back to comp.lang.python


Contents

  intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 20:32 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Nick Timkovich <prometheus235@gmail.com> - 2014-02-25 14:37 -0600
    Re: intersection, union, difference, symmetric difference for dictionaries Skip Montanaro <skip@pobox.com> - 2014-02-25 14:40 -0600
    Re: intersection, union, difference, symmetric difference for dictionaries Peter Otten <__peter__@web.de> - 2014-02-25 21:53 +0100
    Re: intersection, union, difference, symmetric difference for dictionaries Peter Otten <__peter__@web.de> - 2014-02-25 21:58 +0100
    Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 15:03 -0600
      Re: intersection, union, difference, symmetric difference for dictionaries Duncan Booth <duncan.booth@invalid.invalid> - 2014-02-25 22:21 +0000
        Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <tim@thechases.com> - 2014-02-25 16:35 -0600
        Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 16:36 -0600
        Re: intersection, union, difference, symmetric difference for dictionaries Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2014-02-25 22:58 +0000
      Re: intersection, union, difference, symmetric difference for dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-25 23:10 +0000
        Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 20:21 -0600
    Re: intersection, union, difference, symmetric difference for dictionaries Ben Finney <ben+python@benfinney.id.au> - 2014-02-26 08:27 +1100
    Re:intersection, union, difference, symmetric difference for dictionaries Dave Angel <davea@davea.name> - 2014-02-25 16:35 -0500
    Re: intersection, union, difference, symmetric difference for dictionaries Peter Otten <__peter__@web.de> - 2014-02-25 22:54 +0100
    Re: intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 22:02 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 22:03 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Tim Chase <python.list@tim.thechases.com> - 2014-02-25 16:10 -0600
    Re: intersection, union, difference, symmetric difference for dictionaries mauro <mauro@gmail.com> - 2014-02-25 22:11 +0000
      Re: intersection, union, difference, symmetric difference for dictionaries Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-25 22:35 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries MRAB <python@mrabarnett.plus.com> - 2014-02-25 23:07 +0000
      Re: intersection, union, difference, symmetric difference for dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-26 00:37 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Ben Finney <ben+python@benfinney.id.au> - 2014-02-26 10:14 +1100
    Re: intersection, union, difference, symmetric difference for dictionaries MRAB <python@mrabarnett.plus.com> - 2014-02-25 23:25 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries Grant Edwards <invalid@invalid.invalid> - 2014-02-25 20:44 +0000
    Re: intersection, union, difference, symmetric difference for dictionaries John Gordon <gordon@panix.com> - 2014-02-25 20:44 +0000
      Re: intersection, union, difference, symmetric difference for dictionaries Chris Angelico <rosuav@gmail.com> - 2014-03-02 09:29 +1100
    Re: intersection, union, difference, symmetric difference for dictionaries albert@spenarnc.xs4all.nl (Albert van der Horst) - 2014-03-15 17:24 +0000

Page 2 of 2 — ← Prev page 1 [2]


#67074

FromMRAB <python@mrabarnett.plus.com>
Date2014-02-25 23:07 +0000
Message-ID<mailman.7377.1393369652.18130.python-list@python.org>
In reply to#67054
On 2014-02-25 21:27, Ben Finney wrote:
> Peter Otten <__peter__@web.de> writes:
>
>> mauro wrote:
>>
>> > - Dictionaries and sets are both accessed by key
>>
>> but sets have no values
>
> Or rather, sets *only* have values. Dictionaries have keys, sets do not
> have keys.
>
But a dictionary can have duplicate values, a set cannot.

[toc] | [prev] | [next] | [standalone]


#67079

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-02-26 00:37 +0000
Message-ID<530d3758$0$29985$c3e8da3$5496439d@news.astraweb.com>
In reply to#67074
On Tue, 25 Feb 2014 23:07:28 +0000, MRAB wrote:

> On 2014-02-25 21:27, Ben Finney wrote:

>> Or rather, sets *only* have values. Dictionaries have keys, sets do not
>> have keys.
>>
> But a dictionary can have duplicate values, a set cannot.

It is usual to talk about the things stored in dicts and sets using 
slightly different terminology:

  - mappings such as dicts have *key/value pairs*

  - sets have *elements*

The elements of a set are most closely related to the keys of a mapping, 
not the values. Set elements are unique, just like mapping keys. 
Specifically, the keys in a mapping make up a set; the values in a 
mapping do not.

When talking specifically about Python dicts and sets, there is an 
additional restriction: both set elements and dict keys have to be 
hashable.

English being a little sloppy at times, sometimes we also talk about the 
*values of a set*, but here "value" is being used as a synonym for 
"element" and not as a technical term for the thing which mappings 
associate with a key.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#67076

FromBen Finney <ben+python@benfinney.id.au>
Date2014-02-26 10:14 +1100
Message-ID<mailman.7378.1393370084.18130.python-list@python.org>
In reply to#67054
MRAB <python@mrabarnett.plus.com> writes:

> On 2014-02-25 21:27, Ben Finney wrote:
> > Peter Otten <__peter__@web.de> writes:
> >
> >> mauro wrote:
> >>
> >> > - Dictionaries and sets are both accessed by key
> >>
> >> but sets have no values
> >
> > Or rather, sets *only* have values. Dictionaries have keys, sets do
> > not have keys.
> >
> But a dictionary can have duplicate values, a set cannot.

Yes. Your “but” implies you think that contradicts my statement; it
doesn't. So I'm not sure what point you're making.

-- 
 \         “Alternative explanations are always welcome in science, if |
  `\   they are better and explain more. Alternative explanations that |
_o__) explain nothing are not welcome.” —Victor J. Stenger, 2001-11-05 |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#67077

FromMRAB <python@mrabarnett.plus.com>
Date2014-02-25 23:25 +0000
Message-ID<mailman.7379.1393370709.18130.python-list@python.org>
In reply to#67054
On 2014-02-25 23:14, Ben Finney wrote:
> MRAB <python@mrabarnett.plus.com> writes:
>
>> On 2014-02-25 21:27, Ben Finney wrote:
>> > Peter Otten <__peter__@web.de> writes:
>> >
>> >> mauro wrote:
>> >>
>> >> > - Dictionaries and sets are both accessed by key
>> >>
>> >> but sets have no values
>> >
>> > Or rather, sets *only* have values. Dictionaries have keys, sets do
>> > not have keys.
>> >
>> But a dictionary can have duplicate values, a set cannot.
>
> Yes. Your “but” implies you think that contradicts my statement; it
> doesn't. So I'm not sure what point you're making.
>
The keys of a dictionary must be unique, like a set. The values of a
dictionary don't have to be unique, unlike a set.

[toc] | [prev] | [next] | [standalone]


#67359

FromGrant Edwards <invalid@invalid.invalid>
Date2014-02-25 20:44 +0000
Message-ID<leivb2$53$1@reader1.panix.com>
In reply to#67054
On 2014-02-25, mauro <mauro@gmail.com> wrote:
>  Dictionaries and sets share a few properties:
> - Dictionaries keys are unique as well as sets items
> - Dictionaries and sets are both unordered
> - Dictionaries and sets are both accessed by key
> - Dictionaries and sets are both mutables
>
> So I wonder why operations such us intersection, union, difference, 
> symmetric difference that are available for sets and are not available 
> for dictionaries without going via key dictviews. 

What would be the definition of the union, intersection, and
difference of these two dicts?

  {1:'blue', 2:'red'}
  {1:'green', 3:'yellow'}

-- 
Grant Edwards               grant.b.edwards        Yow! I smell like a wet
                                  at               reducing clinic on Columbus
                              gmail.com            Day!

[toc] | [prev] | [next] | [standalone]


#67360

FromJohn Gordon <gordon@panix.com>
Date2014-02-25 20:44 +0000
Message-ID<leivbq$hs0$1@reader1.panix.com>
In reply to#67054
In <G57Pu.24239$Th2.4990@tornado.fastwebnet.it> mauro <mauro@gmail.com> writes:

> - Dictionaries and sets are both accessed by key

As far as I have used sets, they are not accessed by key.

>>> x = set([1, 2, 'buckle my shoe'])
>>> x
set([1, 2, 'buckle my shoe'])
>>> 1 in x
True
>>> 5 in x
False

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [next] | [standalone]


#67382

FromChris Angelico <rosuav@gmail.com>
Date2014-03-02 09:29 +1100
Message-ID<mailman.7543.1393712974.18130.python-list@python.org>
In reply to#67360
On Wed, Feb 26, 2014 at 7:44 AM, John Gordon <gordon@panix.com> wrote:
> In <G57Pu.24239$Th2.4990@tornado.fastwebnet.it> mauro <mauro@gmail.com> writes:
>
>> - Dictionaries and sets are both accessed by key
>
> As far as I have used sets, they are not accessed by key.
>
>>>> x = set([1, 2, 'buckle my shoe'])
>>>> x
> set([1, 2, 'buckle my shoe'])
>>>> 1 in x
> True
>>>> 5 in x
> False

>>> x = {1:3, 2:4, 'buckle my shoe':'spamming the door'}
>>> x
{1: 3, 2: 4, 'buckle my shoe': 'spamming the door'}
>>> 1 in x
True
>>> 5 in x
False

A dict does the exact same thing with its keys as a set does with its
elements. Actually, one of the things that periodically trips me up
when I switch from Pike to Python is that a Python set can't be used
like this:

>>> x = set()
>>> x["test"] = 1
>>> x["foo"] = 1
>>> assert x == {"test","foo"}

Instead, I have to use x.add("test"). In Pike, I can treat a set as if
it were a mapping where every value is simply the integer 1 (Python
could use True instead). Setting it to any truthy value would add it,
setting to any falsy value would remove it. This wouldn't be a huge
change; it certainly wouldn't fundamentally change the set type -
because it *is* working with keys.

Hmm. Actually, it shouldn't be too hard to subclass and add that. Lessee.

class set(set):
    def __setitem__(self, key, state):
        if state: self.add(key)
        else: self.remove(key)
    def __getitem__(self, key):
        return key in self

I might need to toss that into site.py or something. It'd work as long
as I don't use set literal notation anywhere.

ChrisA

[toc] | [prev] | [next] | [standalone]


#68368

Fromalbert@spenarnc.xs4all.nl (Albert van der Horst)
Date2014-03-15 17:24 +0000
Message-ID<53248cb0$0$25075$e4fe514c@dreader37.news.xs4all.nl>
In reply to#67054
In article <G57Pu.24239$Th2.4990@tornado.fastwebnet.it>,
mauro  <mauro@gmail.com> wrote:
> Dictionaries and sets share a few properties:
>- Dictionaries keys are unique as well as sets items
>- Dictionaries and sets are both unordered
>- Dictionaries and sets are both accessed by key
>- Dictionaries and sets are both mutables
>
>So I wonder why operations such us intersection, union, difference,
>symmetric difference that are available for sets and are not available
>for dictionaries without going via key dictviews.

This is a plain bad idea.
Dictionaries correspond to the mathematical concept of a mapping.
A mapping (or a function) is a set in math, as everything is a set.
It is a subset of the product set of two set A and B where
there is exactly one pair for each a in A.

No sane mathematician talks about unions, intersections etc.
of those sets, though clearly they are well defined.
OTOH there is a very rich vocabulary specific for the properties
of functions.

So dear mauro do as everybody does, as soon as you've invented
something useful related to dicts, you'll discover that it correspond
to an age old mathematical concept. It is unwise not to borrow its
name.

Those old geezers, Chebychov, Euler, Laplace, Fourier had their
marbles in a row. It is hard to outsmart them.

Groetjes Albert
-- 
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web