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


Groups > comp.lang.python > #84016

Re: Hashed lookups for tabular data

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.029
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; 'duplicate': 0.07; 'key.': 0.09; 'lookup': 0.09; 'seen,': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; "wouldn't": 0.14; '1:13': 0.16; 'bars': 0.16; 'container.': 0.16; 'dictionaries': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'idempotent.': 0.16; 'keyerror': 0.16; 'shared.': 0.16; 'simplified': 0.16; 'subject:tabular': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'fit': 0.20; 'memory': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'decide': 0.24; 'cc:2**0': 0.24; 'query': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'am,': 0.29; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'program,': 0.31; 'grouping': 0.31; 'keys': 0.31; 'option.': 0.31; 'skip:q 20': 0.31; 'tuples': 0.31; 'run': 0.32; 'cases': 0.33; 'not.': 0.33; 'actual': 0.34; 'could': 0.34; 'basic': 0.35; 'classes': 0.35; 'something': 0.35; 'objects': 0.35; 'one,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'accessing': 0.36; 'right?': 0.36; 'subject:data': 0.36; 'done': 0.36; 'similar': 0.36; 'should': 0.36; 'requirements': 0.37; 'list': 0.37; 'stable': 0.38; 'anything': 0.39; 'does': 0.39; 'though,': 0.39; 'enough': 0.39; 'either': 0.39; 'remove': 0.60; 'easy': 0.60; 'matter': 0.61; 'providing': 0.61; 'simply': 0.61; "you're": 0.61; 'back': 0.62; "you'll": 0.62; "you've": 0.63; 'places': 0.64; 'more': 0.64; '20,': 0.68; 'of:': 0.68; 'surprise': 0.74; '2015': 0.84; 'presumably': 0.84; 'safe.': 0.84; 'utilized': 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=0D0xYSh05oxiED3Jvly0XtJIp1f3kwki4NjsVRi8KAc=; b=NsS/uONZEK+R5FIeOJ8nsfDmonOjon/gXhO+eaGng04XFhiNIplTfPdQKdAb3fsGOC rAwOPQRtEukbJiB0Qet7CFcqPgayxGVAkkCLTfSE7amt+WEKMRXC7NW7OllINZnk59lD gUQAjHxFvCKi2dVVobPHdFqFpjsUN2fEL2FyBy1UMV3v3utmRvf35zBNpefnHKcJbdJE UOy+KSSnwd1HW9rSbaouMbz9lr4FnY5tqhDuOg4cGsI4oJIYpuBjh8jOmy8VtDmw1fYp GebFcMylhHymTIbroTD2M1mtpCbM76ExaeO0AOakf1IEvoSwU9BYTZJvqbx2LMb5WkUf +4eA==
MIME-Version 1.0
X-Received by 10.229.225.68 with SMTP id ir4mr48199381qcb.23.1421677606474; Mon, 19 Jan 2015 06:26:46 -0800 (PST)
In-Reply-To <84c6e1d5671842038e81994478fb5476@exch.activenetwerx.com>
References <84c6e1d5671842038e81994478fb5476@exch.activenetwerx.com>
Date Tue, 20 Jan 2015 01:26:46 +1100
Subject Re: Hashed lookups for tabular data
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17853.1421677615.18130.python-list@python.org> (permalink)
Lines 46
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1421677615 news.xs4all.nl 2952 [2001:888:2000:d::a6]:47181
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:84016

Show key headers only | View raw


On Tue, Jan 20, 2015 at 1:13 AM, Joseph L. Casale
<jcasale@activenetwerx.com> wrote:
> No surprise the data originates from a database however the data is utilized in
> a recursive processing loop where the user accessing the data benefits from a
> simplified and quick means to access it. Currently its done with classes that
> resemble named tuples equating to a row, but it goes pear shaped with the outer
> container. Grouping these similar objects and providing the sql like query into
> the collection in some cases is O(n)...

So presumably your data's small enough to fit into memory, right? If
it isn't, going back to the database every time would be the best
option. But if it is, can you simply keep three dictionaries in sync?

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)

You can now look up anything by any key. If you use the quux_dict,
you'll get back a list (zero elements if you've never seen one,
otherwise all elements ever seen, in arbitrary order). Adding, in this
way, is idempotent.

Finding an item by foo or bar is easy - use either of:
row = foo_dict["asdf"] # will raise KeyError if it isn't there
row = foo_dict.get("qwer") # will return None if it isn't there

Finding items by quux is a matter of iteration:
for row in quux_dict["zxcv"]:
    do something

It's up to you to decide whether it's an error to get back an empty set or not.

Does this give a basic idea of what you can do?

The memory requirements wouldn't be much more than your existing data.
You have three lookup dictionaries, but the actual keys and values
would be shared. The main consideration is that you'd need to update
in lots of places if you remove a row. If you're working with stable
data across one run of the program, though, that should be safe.

ChrisA

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


Thread

Re: Hashed lookups for tabular data Chris Angelico <rosuav@gmail.com> - 2015-01-20 01:26 +1100

csiph-web