searching in list

V

vino19

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
 
D

Dave Angel

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

Usual answer is to use a dict. You can convert a list of tuples to a
dict by dict(mylist)

Second choice is to keep the list sorted, and use the bisect module.
But usually a dictionary is better.

DaveA
 
C

Chris Angelico

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
 
C

Chris Angelico

Thanks.

It seems that dictionary is a sorted list of tuples, so the procedure of searching an element is quite quick.

Not sorted - it's hashed, so it's even faster. Yep, sounds like a
dictionary is everything you want!

Chris Angelico
 
I

Ian Kelly

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top