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


Groups > comp.lang.python > #6660

Re: searching in list

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.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; 'answering': 0.09; 'argument,': 0.09; 'calculates': 0.09; 'storing': 0.09; 'pm,': 0.10; 'def': 0.12; 'received:209.85.214.174': 0.14; 'received:mail- iw0-f174.google.com': 0.14; 'wrote:': 0.14; '(note': 0.16; 'angelico': 0.16; 'cache.': 0.16; 'calculating': 0.16; 'dictionary:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'immutable': 0.16; 'naive': 0.16; 'simple:': 0.16; 'algorithm': 0.16; 'argument': 0.16; 'cache': 0.16; 'list?': 0.16; 'mon,': 0.17; 'subject:list': 0.19; 'once,': 0.19; 'header:In-Reply-To:1': 0.21; 'asked': 0.24; 'function': 0.25; 'demonstrate': 0.26; 'example': 0.27; 'message- id:@mail.gmail.com': 0.28; 'thanks': 0.28; 'received:209.85.214': 0.28; 'consist': 0.29; 'bit': 0.30; 'fairly': 0.30; 'tuples': 0.30; 'calling': 0.31; 'does': 0.33; 'to:addr:python-list': 0.33; 'list': 0.33; 'actually': 0.33; 'it?': 0.33; 'things': 0.33; 'chris': 0.34; 'there': 0.35; 'store': 0.35; 'else': 0.35; 'received:google.com': 0.37; 'received:209.85': 0.37; 'could': 0.38; 'but': 0.38; 'data': 0.38; 'ok,': 0.38; 'subject:: ': 0.38; 'called': 0.39; 'received:209': 0.39; 'add': 0.39; 'to:addr:python.org': 0.39; 'meaning': 0.40; 'best': 0.60; '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:date :message-id:subject:from:to:content-type; bh=j+oapgQxSUmwq0VcKoKZ8IggCGDYN/iFlziVBuUywDw=; b=r62uvvtxqUblG2/kLg+FglEiMG22gEKCeAvoqnmxdKZa4/yHhCuz92X3F4w2KkLGxz FP+bZ4oHq83UNlsjUTGLTPqbf/oFX3r3+GkgMxArYufQ+iTV7ZHfLl9p2x4SB7DOYsfJ Y/8C922gbdhFjFMX61wAcHHVocVRh+d3EsJpg=
DomainKey-Signature a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=XAhttCzjeYD9PV1JQfUY4YFOo5SRwa7Njj2KpPq/KM/5343giOA12qq18ELyZwWdTo IN2OCpiJ3KFBn1+fanOsGkqYcBZSm6GB6IZxObIIs19unx3qcaMiXut6vdqwfnATPfAi 7MtUyFLLuBDITXOOok8AdYWV4cRbH0f5io240=
MIME-Version 1.0
In-Reply-To <cec1aa77-86b3-4bff-8286-a84c823750d2@glegroupsg2000goo.googlegroups.com>
References <cec1aa77-86b3-4bff-8286-a84c823750d2@glegroupsg2000goo.googlegroups.com>
Date Mon, 30 May 2011 23:41:00 +1000
Subject Re: searching in list
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.2266.1306762863.9059.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 82.94.164.166
X-Trace 1306762864 news.xs4all.nl 49183 [::ffff:82.94.164.166]:54908
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:6660

Show key headers only | View raw


On Mon, May 30, 2011 at 10:58 PM, vino19 <vinograd19@gmail.com> wrote:
> I want to make a function that is called only once per one argument. I mean I want to store data of function calling to prevent calling it again if there is no need.
> How to make it? For example I can make a global list that just consist of tuples
> [(arg1, res1), (arg2, res2), ...]. Ok, how to search if an arg135 in that list?
>
> Thanks for answering

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.)

Chris Angelico

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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