?
Solved

lists in python

Posted on 2007-08-12
10
Medium Priority
?
172 Views
Last Modified: 2010-04-16
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




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.



0
Comment
Question by:Troudeloup
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 15

Expert Comment

by:efn
ID: 19679657
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?

0
 

Author Comment

by:Troudeloup
ID: 19679670
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?
0
 
LVL 15

Expert Comment

by:efn
ID: 19679743
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?
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:Troudeloup
ID: 19679748
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.
0
 
LVL 15

Accepted Solution

by:
mish33 earned 2000 total points
ID: 19680077
data = [
  (x0,y0,a0),
  (x1,y1,a1),
  (x2,y2,a2),
  (x3,y3,a3),
  # ...
  ]
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
0
 
LVL 15

Expert Comment

by:efn
ID: 19680096
I still don't understand the question.
0
 
LVL 29

Expert Comment

by:pepr
ID: 19682187
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?
0
 

Author Comment

by:Troudeloup
ID: 19688264
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?
0
 
LVL 29

Expert Comment

by:pepr
ID: 19689634
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).
0
 
LVL 29

Expert Comment

by:pepr
ID: 19689652
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).
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month17 days, 2 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question