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


Groups > comp.lang.python > #67382

Re: intersection, union, difference, symmetric difference for dictionaries

References <G57Pu.24239$Th2.4990@tornado.fastwebnet.it> <leivbq$hs0$1@reader1.panix.com>
Date 2014-03-02 09:29 +1100
Subject Re: intersection, union, difference, symmetric difference for dictionaries
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.7543.1393712974.18130.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

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

csiph-web