• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

lists in python

I need a list to store some data,

basically I do this:

x,y  = function (entry_for_x,y)            

if function_if ( x,y):
    return function (entry_for_angle)

and the function reads from a list like this

list                       = { x, y, angle,
                                 x, y, angle,
                                 x, y, angle,
                                 x, y, angle,
                                 x, y, angle,
                                 x, y, angle,
                                 x, y, angle,
                                 x, y, angle,


so overall, the list contains the (x,y) for the function to check, and then if true then the angle for that (x,y)
combo is returned.

like this:

for a in range(0,200, 1):
   if function(list(a, 1)):
       return function(list(a, 2))

in the example above,    function(list(a, 1))   would be doing true/false verification with x,y read from the list
and then if true angle (the third item)  would be returned

is this clear?   please don't hesitate to ask for clarification.

  • 3
  • 3
  • 3
  • +1
1 Solution
Sorry, it's not clear to me.  I'll try to restate the problem.

You have a constant list where each entry contains three numbers.  Given two numbers, if there is a list entry where the first two numbers match the two given numbers, you want a function to return the third number in that list entry.

Is that right?

If there is no match, what should the function return?  The null value None?

TroudeloupAuthor Commented:
yeah yeah.

the first two numbers are used to calculate something and then if the result of that calculation fits a criteria then the third number is returned and that ends the function which use this list.

if the calculation of first two numbers doesn't match the criteria then the next entry comes in,

ie, the next (x,y).

the process continues until a (x,y) fits the criteria and the angle (the third value) is returned or this function prints error message.

does this sound better?
Thanks, that's somewhat clearer, but I'm still not sure I understand.  I will try to restate it again.

There is a finite source of pairs of input numbers.  That is, a function can read pairs of numbers up to some point where there aren't any more.

A function is to read pairs of numbers until it finds the first pair of numbers that satisfies a condition C.  If it finds one, it returns a single value.  If no input pair satisfies the condition, the function is to put out an error message.  What should it return in that case?  Does it matter?

Condition C involves first a calculation with the two numbers and a test of the result.  If the test passes, the two numbers are used to look up a result in a list as I described before, and the third element of the list entry is returned as the function value.

It's not clear what should happen if the calculation result passes but the function can't find a matching list entry.  This could be treated as a failure to satisfy condition C, or perhaps the calculation and the list are designed to guarantee that this will never happen.  Can this happen?  If so, what should the function do in this case?
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

TroudeloupAuthor Commented:
an entry in this list is a tuple of 3

first two in this tuple are used find out if it passes the calculation., if it does, the third value is returned,
it's impossible for no right pair of to be found.
data = [
  # ...
def check(aa):
  for x,y,a in data:
    if func1(x,y,aa):
      return a
def func1(x,y,a):
  # don't forget to define that function like
  return x<=a<y
I still don't understand the question.
It souns like a confusion for me ;)  Don't you want to cache the results of some expensive computation? Should not this be some optimization to get the angle faster if it was earlier (pre)calculated. If yes, then list is not the good data structure. Possibly a dictionary would be better in such case.

Or, what the list represents? Is the order of items (tuples) important?
TroudeloupAuthor Commented:
yes it is meant to get angle faster.

if the first two values fits, then angle is a the anwser.

as there are only ~400 entries, this is good enough.

 I am not sure what a dictionary would do though?
Save the following example into fun.py and run it:

class AngleCalculator:

    def __init__(self):
        self.angleCache = {}
    def __calculateAngle(self, x, y):
        '''Private method for angle calculation -- here faked (string instead).'''
        return 'angle for %i and %i' % (x, y)

    def angle(self, x, y):
        if (x, y) in self.angleCache:
            print 'The angle for %i, %i is cached' % (x, y)
        return self.angleCache.setdefault( (x, y), self.__calculateAngle(x, y))
# Create the instance of the calculator with cache.        
calc = AngleCalculator()

# The following calculations are not cached.
for x in xrange(10):
    for y in xrange(10):
        print calc.angle(x, y)

# The following results come from cache because they were
# already calculated earlier.      
for x in xrange(3):
    for y in xrange(3):
        print calc.angle(x, y)

The method __calculateAngle starts with two underscores to make it clear
that it is private and should not be called directly. It does the calculation
(here it returns the string that displayes also the passed arguments
only to show that something happened). The method angle() returns
the result, but the object decides whether it will be calculated or not.
The decision is made on the fact that the tuple (x, y) is present as a key
to the dictionary or not. See the documentation for the setdefault() dictionary
method (http://docs.python.org/lib/typesmapping.html#l2h-294).
The lines

     if (x, y) in self.angleCache:
            print 'The angle for %i, %i is cached' % (x, y)

are there only to visualize the activity. They will be removed from the code.

The solution with the list (as you wanted initially and that you accepted from mish33) may work but it will be very slow in comparison with using the dictionary. If in doubt, try to compare both solutions (i.e. measure).
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 3
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now