Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'assign': 0.07; 'duplicate': 0.07; 'python3': 0.07; 'cc:addr:python-list': 0.11; 'python': 0.11; 'jan': 0.12; 'bars': 0.16; 'collections': 0.16; 'dictionaries': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'subject:tabular': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'hacking': 0.19; '>>>': 0.22; 'memory': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'simpler': 0.24; 'cc:2**0': 0.24; 'references': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'dec': 0.30; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'keys': 0.31; 'object.': 0.31; 'skip:q 20': 0.31; 'themselves': 0.32; 'linux': 0.33; 'actual': 0.34; 'could': 0.34; 'objects': 0.35; 'requirement': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'subject:data': 0.36; 'more': 0.64; '20,': 0.68; '2014,': 0.84; '2015': 0.84; 'usage.': 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=aGVdaHezlDYFmxqwb2dZ8i/Zrpbl5D8bigKGmbvZh0I=; b=ovntti8mhS68sguJItdbGZf+/IuQctd5kxFeRGOkn41lMOQ0RvR/XdlmtqSmB9QHjh JpRAzDwUyyw2Ig3qTsZf2tsCJo0LNnNiOiTZTIzjzxh4QrwZtuw2zonhiKDT53YVl5Cx y7omKbiWeWKnTv3VbijMKfc9pl8GpXZc8ZzirL6shU6xfwtLCsuRloU/wLMIvlZD7Z53 po2XAwKgIj7LYT0QOqkwIDFDaiiwlGgVrdIFBgx4jrSG+fL4H1oOV0/px8p7SMOsXgFR T0uY/aNaUps/rVpiisMRO7A8R7H6sfV4jaF++rA/+O2PjiKnT/M+BgLVQ1dPy8A3sVja tCPg== MIME-Version: 1.0 X-Received: by 10.140.21.229 with SMTP id 92mr36904292qgl.33.1421687877302; Mon, 19 Jan 2015 09:17:57 -0800 (PST) In-Reply-To: <1421687345419.39659@activenetwerx.com> References: <84c6e1d5671842038e81994478fb5476@exch.activenetwerx.com> <1421687345419.39659@activenetwerx.com> Date: Tue, 20 Jan 2015 04:17:57 +1100 Subject: Re: Hashed lookups for tabular data 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1421687884 news.xs4all.nl 2835 [2001:888:2000:d::a6]:59636 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:84020 On Tue, Jan 20, 2015 at 4:09 AM, Joseph L. Casale wrote: >> row = (foo, bar, quux) # there could be duplicate quuxes but not foos or bars >> foo_dict = {} >> bar_dict = {} >> quux_dict = collections.defaultdict(set) >> >> foo_dict[row[0]] = row >> bar_dict[row[1]] = row >> quux_dict[row[2]].add(row) > > This is actually far simpler than I had started imagining, however the row data > is duplicated. I am hacking away at an attempt with references to one copy of > the row. As long as you start with an actual tuple that you then assign in that way, they will all be references to one copy of the row. Here's a concrete example: $ python3 Python 3.5.0a0 (default:1c51f1650c42+, Dec 29 2014, 02:29:06) [GCC 4.7.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> row = ("abc","def","ghi") >>> foo_dict = {} >>> bar_dict = {} >>> import collections >>> quux_dict = collections.defaultdict(set) >>> foo_dict[row[0]] = row >>> bar_dict[row[1]] = row >>> quux_dict[row[2]].add(row) >>> id(foo_dict["abc"]) 140127396717840 >>> id(bar_dict["def"]) 140127396717840 >>> id(next(iter(quux_dict["ghi"]))) 140127396717840 The IDs of the objects prove that they're actually all the same object. The memory requirement for this is just what the dictionaries themselves require; their keys and values are all shared with other usage. ChrisA