Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '(python': 0.07; 'elements.': 0.07; '*is*': 0.09; 'key.': 0.09; 'literal': 0.09; 'toss': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; "wouldn't": 0.14; 'change;': 0.16; 'dict': 0.16; 'dictionaries': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'hmm.': 0.16; 'instead).': 0.16; 'key):': 0.16; 'keys.': 0.16; 'notation': 0.16; 'set()': 0.16; 'subclass': 0.16; 'subject:dictionaries': 0.16; 'x["test"]': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'feb': 0.22; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'certainly': 0.24; 'integer': 0.24; "shouldn't": 0.24; 'cc:2**0': 0.24; 'switch': 0.26; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'sets': 0.30; 'message- id:@mail.gmail.com': 0.30; 'that.': 0.31; '>>>>': 0.31; 'assert': 0.31; 'keys': 0.31; 'writes:': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'could': 0.34; "can't": 0.35; 'received:google.com': 0.35; 'add': 0.35; 'false': 0.36; 'too': 0.37; 'mapping': 0.38; 'does': 0.39; 'skip:x 10': 0.40; 'remove': 0.60; 'john': 0.61; 'simply': 0.61; '26,': 0.68; 'periodically': 0.68; 'actually,': 0.84; "it'd": 0.84; 'pike': 0.84; 'sets,': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=cmKIv65ABICi14d6vBIDhsQzM85U20OtRGHRjvBFxYc=; b=FjQYwZw1ro7YzsID6MHsWUbbVpCbvSH0bbL+5CGTFWYi/NsZPuRLoPnWnu1X8UCMeo pH9i0vf184BHIFHX+HTcwpfJd2i8O/yFuZc62bZH+yam7+903mvgo9nxaSTttfYklymL gI+WGwQ8DRTQZxlz+gGpcG5H9CXJJreb3uTBalP91w8bECi3ONZPilqvhPNzeXhA++eX BmWSVBslqcJAybXotVeZQI9gy8+ixvYXa9/lBelSSz99TwRraq+FbqgOypIVMg9w+o8w Xam/6Or3f+/JdYt14YaektZ+bjFvnv1zCr8CkUUlY+s/AHqAOsL54CwzJMhH3Or9DM6H NCkA== MIME-Version: 1.0 X-Received: by 10.68.33.106 with SMTP id q10mr71480pbi.132.1393712965423; Sat, 01 Mar 2014 14:29:25 -0800 (PST) In-Reply-To: References: Date: Sun, 2 Mar 2014 09:29:25 +1100 Subject: Re: intersection, union, difference, symmetric difference for dictionaries From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 53 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1393712974 news.xs4all.nl 2849 [2001:888:2000:d::a6]:46563 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67382 On Wed, Feb 26, 2014 at 7:44 AM, John Gordon wrote: > In mauro 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