Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Marco Kaulea Newsgroups: comp.lang.python Subject: Re: Set Operations on Dicts Date: Mon, 8 Feb 2016 13:33:56 +0100 Lines: 38 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de P5SW3zmZFFCAZD6nRqZy+QDGBYVFJtk5IYKT9rR8uoQw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.030 X-Spam-Evidence: '*H*': 0.94; '*S*': 0.00; 'lookup': 0.09; 'precedence': 0.09; 'restriction': 0.09; 'skip:f 30': 0.15; '2016': 0.16; 'argument.': 0.16; 'did.': 0.16; 'keys.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'sense,': 0.16; 'slow,': 0.16; 'wrote:': 0.16; 'basically': 0.18; 'keys': 0.22; 'mind.': 0.22; 'seems': 0.23; 'feb': 0.23; 'sets': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'message- id:@mail.gmail.com': 0.27; 'values': 0.28; 'arithmetic': 0.29; 'dictionary': 0.29; 'compared': 0.30; 'useful': 0.33; 'common': 0.33; 'received:google.com': 0.35; 'could': 0.35; 'done': 0.35; 'quite': 0.35; 'something': 0.35; 'express': 0.35; 'sometimes': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'cases': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'expect': 0.37; 'thought': 0.37; 'received:209': 0.38; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'your': 0.60; '&': 0.61; 'default': 0.61; 'more': 0.63; 'different': 0.63; 'union': 0.67; 'useful.': 0.72; 'optimized.': 0.84; 'hassle': 0.91; 'subject:Set': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=5j0efwnlwmYZz7e+2FXCPD+iUMBm4VWplu1B8YxCeVk=; b=fQ1nid6fB/hr8aF3x71jXonLXeO17j3N8/wjQac/OvXpfMQRLs9RFakF/Dieh2tshu wUfxDvneNdQKoqI42wbODd+0F2bd3JxUDlDOGMya3IUTqpBIRxMfq9YINWt/l6B621JW YO57c/hwdWNO46FzBmmaOJxFNCwQsmtXpnNDDfnAhNRaX91qbXZ3RzSnFdLlx0Ubvd73 jXKgQLwL/lBLaa7Ql6KIxRi49jVmjWvj80k4BCvLR4Ko9Kgi4AXfoYkPV7GGvYHpMsS4 ygHclpalyuneYQEp8uPUoZgE+to+ZOGkHr4SkK787es+RXVpA+4HsSNimEXsLW77uzPW i/EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=5j0efwnlwmYZz7e+2FXCPD+iUMBm4VWplu1B8YxCeVk=; b=Bf/9PSGUKB6DOfcQSJbWfoPflBmxmU79XZ5h8UozhmtQbQgK/8FYaf6FpDjuQx1TnI Z1kG6fQcOX9WSLNt+ouc24bCIH96bDJS06uAMoIJ+VFchJ+hHKExPLv6Q3dOE3IZ4r9t /hLiX1afFcsMr/ArcHg5HsKniiulLm52NUGFj7xLY/IuKHbI+thHKQwi8OIT0Z9EkT+w 6wI3QAOx3v0LhdAFDjogqZ+hrKkO8qv87TS+CfB7dVOMTgsFnWQ4bmccmIVWtZYxR4lU 7UWmvW8vDOqsnajyZqcxk3LSTzmo693GHnOgMY5/4RIkgl92PvA3VVGqwRAq12+Jhalq p2Cg== X-Gm-Message-State: AG10YORw75LSfFKnyjcC5E3WgfPJpIYaAL+nuczaaFMvRmh0A8f/KX6pxFnzzLS1c/ZkToN1bX/h6PoNvydhlw== X-Received: by 10.60.54.232 with SMTP id m8mr24075517oep.34.1454934855653; Mon, 08 Feb 2016 04:34:15 -0800 (PST) In-Reply-To: X-Mailman-Approved-At: Mon, 08 Feb 2016 07:37:54 -0500 X-Content-Filtered-By: Mailman/MimeDel 2.1.21rc2 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102667 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")