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


Groups > comp.lang.python > #6666

Re: searching in list

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
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; 'arguments': 0.05; 'instance,': 0.05; 'way:': 0.05; 'cached': 0.07; 'dictionary': 0.07; 'ret': 0.07; 'python': 0.08; 'argument,': 0.09; 'arguments.': 0.09; 'decorator': 0.09; 'storing': 0.09; 'def': 0.12; 'am,': 0.14; 'wrote:': 0.14; '(note': 0.16; '3.2,': 0.16; 'angelico': 0.16; 'cache.': 0.16; 'caching': 0.16; 'calculating': 0.16; 'dictionary:': 0.16; "function's": 0.16; 'immutable': 0.16; 'naive': 0.16; 'simple:': 0.16; 'algorithm': 0.16; 'argument': 0.16; 'cache': 0.16; 'mon,': 0.17; 'subject:list': 0.19; 'once,': 0.19; 'header:In-Reply-To:1': 0.21; 'stores': 0.23; '\xa0if': 0.23; 'received:209.85.161.46': 0.23; 'received:mail- fx0-f46.google.com': 0.23; 'asked': 0.24; 'function': 0.25; 'demonstrate': 0.26; 'received:209.85.161': 0.26; 'example': 0.27; 'message-id:@mail.gmail.com': 0.28; 'fairly': 0.30; 'email name:': 0.30; 'modifying': 0.30; 'does': 0.33; 'to:addr:python-list': 0.33; 'things': 0.33; 'chris': 0.34; 'using': 0.35; '8bit%:8': 0.36; 'received:google.com': 0.37; 'received:209.85': 0.37; 'could': 0.38; 'but': 0.38; 'subject:: ': 0.38; 'received:209': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'meaning': 0.40; 'best': 0.60; 'storage': 0.66; '100': 0.73; '30,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type:content-transfer-encoding; bh=qxkpKJXK+paihrhHOFNDTWwyqBnYILXFT0mcFik7wlE=; b=EbSDFSqGnmbLKiG7XmCzOVlMOpVUFdm3Mx2NAuWOoq9NPTH4LucZ46te0rkeiRJ/v6 boSoezPrWMYQyOWHnFL8OZgAqlNIby7lOJF6Ndeg1mUSAu+csUKG+pkgp+mJy77I53M7 C1nkTr3NrImOA1KMSEurYjngAt/2JAG4TXe3U=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=XeqB6+mVQDMeSLiuZ0ikPN3KK1IRpismwOWvoaubHSDPzmOw/F1Q4w9EPylHETm2L/ kWHGoyRSrzqzCJ4Akam+u9OQFMWPnvCUT+m5R5LxT4XMTfGEfTxetGUK3EU3uUlfYp4I JSMs50N8VU82Nk/8JnHL5U46Y/SaPq21lKrOI=
MIME-Version 1.0
In-Reply-To <BANLkTikQ8cR72PPqxrv5ajFrQnDBg5NN5A@mail.gmail.com>
References <cec1aa77-86b3-4bff-8286-a84c823750d2@glegroupsg2000goo.googlegroups.com> <BANLkTikQ8cR72PPqxrv5ajFrQnDBg5NN5A@mail.gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 30 May 2011 10:32:27 -0600
Subject Re: searching in list
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2274.1306773179.9059.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 82.94.164.166
X-Trace 1306773179 news.xs4all.nl 49175 [::ffff:82.94.164.166]:42449
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6666

Show key headers only | View raw


On Mon, May 30, 2011 at 7:41 AM, Chris Angelico <rosuav@gmail.com> wrote:
> If you're always going to look them up by the argument, the best way
> would be to use a dictionary:
> cache={arg1: res1, arg2: res2, ...}
>
> Then you can search with a simple: cache[arg135]
>
> You can add things with: cache[arg135]=res135
>
> This works best if the argument is an immutable and fairly simple
> type. For instance, you could calculate Fibonacci numbers in a naive
> and straightforward way:
>
> def fib(n,cache={}):
>  if n in cache: return cache[n]
>  ret=n if n<2 else fib(n-1)+fib(n-2) # This bit actually calculates fib(n)
>  cache[n]=ret
>  return ret
>
> Of course, this is a poor algorithm for calculating Fibonacci numbers,
> but it does demonstrate the cache. It's recursive, meaning that if it
> has fib(50) in its cache and it is asked for fib(60), it will find and
> make use of the cached element.
>
> (Note the sneaky way of storing a cache dictionary. A function's
> default arguments are evaluated once, so this will use the same
> dictionary for every call.)

If you're using Python 3.2, the functools.lru_cache decorator will do
the caching for you, and without modifying the function arguments.  By
default it stores only the 100 most recent results, but you can make
the storage unbounded by passing in the argument maxsize=None.  Then
the Fibonacci example becomes:

@functools.lru_cache(maxsize=None)
def fib(n):
    if n < 2:
        return n
    return fib(n-2) + fib(n-1)

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


Thread

searching in list vino19 <vinograd19@gmail.com> - 2011-05-30 05:58 -0700
  Re: searching in list Dave Angel <davea@ieee.org> - 2011-05-30 09:27 -0400
  Re: searching in list Chris Angelico <rosuav@gmail.com> - 2011-05-30 23:41 +1000
  Re: searching in list Ian Kelly <ian.g.kelly@gmail.com> - 2011-05-30 10:32 -0600

csiph-web