i need some help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sippy99
    New Member
    • Feb 2008
    • 7

    i need some help

    I am a beginner in python language and I have the following question:

    i am trying to define something, let say X, under certain limits and I am not sure how to do that.

    basically i want to do the following: if X is less than 200 and greater than 100, then continue, else stop.

    can someone provide some kind of starting point. i know i have to do a for loop, but not exactly sure how to go about it.

    thanks in advance
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by sippy99
    I am a beginner in python language and I have the following question:

    i am trying to define something, let say X, under certain limits and I am not sure how to do that.

    basically i want to do the following: if X is less than 200 and greater than 100, then continue, else stop.

    can someone provide some kind of starting point. i know i have to do a for loop, but not exactly sure how to go about it.

    thanks in advance
    You don't need a for loop. Test for a condition with an if statement. If condition is met, do something. Otherwise, do something else.[code=Python]>>> x = 50
    >>> if 100 < x < 200:
    ... print 'In range'
    ... else:
    ... print 'Out of range'
    ...
    Out of range
    >>> [/code]

    Comment

    • sippy99
      New Member
      • Feb 2008
      • 7

      #3
      Originally posted by bvdet
      You don't need a for loop. Test for a condition with an if statement. If condition is met, do something. Otherwise, do something else.[code=Python]>>> x = 50
      >>> if 100 < x < 200:
      ... print 'In range'
      ... else:
      ... print 'Out of range'
      ...
      Out of range
      >>> [/code]
      sorry, i should have mentioned the whole thing because I am still unsure.
      My x here is actually a list of values. And i have 2 lists of values that correspond to an id and I am trying to define parameters using the two lists. i just don't know the proper notation and such to write the code:
      i am trying the following with no sucess:

      >>> x = fields [1] -- my list
      >>> y = filelds [2] -- 2nd list
      >>> if 100 <= x <= 200
      >>>>> continue
      >>>>>>> if 5 <= y <= 8
      >>>>>>> print id (print the id that has the defined parameters)

      Thanks again for any help

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Originally posted by sippy99
        sorry, i should have mentioned the whole thing because I am still unsure.
        My x here is actually a list of values. And i have 2 lists of values that correspond to an id and I am trying to define parameters using the two lists. i just don't know the proper notation and such to write the code:
        i am trying the following with no sucess:

        >>> x = fields [1] -- my list
        >>> y = filelds [2] -- 2nd list
        >>> if 100 <= x <= 200
        >>>>> continue
        >>>>>>> if 5 <= y <= 8
        >>>>>>> print id (print the id that has the defined parameters)

        Thanks again for any help
        You will need a for loop after all. I take it that fields is a list of lists. Maybe something like the following?[code=Python]xList = fields[1]
        yList = fields[2]

        for i, item in enumerate(xList ):
        if 100 <= item <= 200:
        if 5 <= yList[i] <= 8:
        print id[/code]

        Comment

        • sippy99
          New Member
          • Feb 2008
          • 7

          #5
          Originally posted by bvdet
          You will need a for loop after all. I take it that fields is a list of lists. Maybe something like the following?[code=Python]xList = fields[1]
          yList = fields[2]

          for i, item in enumerate(xList ):
          if 100 <= item <= 200:
          if 5 <= yList[i] <= 8:
          print id[/code]

          I tried this but it didn't work. Actually, my ids = fields [0] (which correspond to fields [1] and fields [2] that I am using for defining parameters), so will that change anything? Also, this code runs and selects the id with the defined parameters. What if I had many different parameters? Will they just have thier own blocks in the code?

          thanks again.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            Originally posted by sippy99
            I tried this but it didn't work. Actually, my ids = fields [0] (which correspond to fields [1] and fields [2] that I am using for defining parameters), so will that change anything? Also, this code runs and selects the id with the defined parameters. What if I had many different parameters? Will they just have thier own blocks in the code?

            thanks again.
            You can create a function whose arguments are the parameters. I don't understand your data. If you would post a representative sample, it may help.

            Comment

            • sippy99
              New Member
              • Feb 2008
              • 7

              #7
              Originally posted by bvdet
              You can create a function whose arguments are the parameters. I don't understand your data. If you would post a representative sample, it may help.
              Here is an example that reflects what I am doing:
              Id = fields [0]
              Height = fields [1]
              Length = fields [2]
              Width = fields [3]

              Then I have the parameters:
              TypeX = (height range, length range, width range) ~ general

              For example;
              Type1 = (1.1 - 2.1, 3.2 - 5.3, .2 - .5)
              Type2 = (1.0 - 2.0 , 2.3 - 4.2 , 1.2 - 2.8)

              I want to write a code (but not sure how), so that it runs through my file which has the list (id, Height, Length, Width) and spits out how many Type 1 I have and Type 2, etc. I know that I need a for loop, because the code will first have to see if the height range matches, then if the length range matches, and then the width range. If everything matches, the id should be the output. Would you use
              Code:
              else
              statement when lets say the height range doesn't match for Type 1, but then you want to see if it matches Type2.
              I hope this explains little better of what I am trying to achieve. Again, I appreciate your help.

              Comment

              • dshimer
                Recognized Expert New Member
                • Dec 2006
                • 136

                #8
                If it is just an external file of values in some kind of table, you may be getting too complicated with the data structure on the way in, would it be easier to just read in a line then check it's values on the way through the file. Along with bvdet's idea of a function to check the ranges. If I have a file that has id, height, width, length and looks something like.
                a 1 2 3
                b 3 4 5
                c 5 6 7
                reading and parsing the data could be as simple as
                Code:
                f=open('/tmp/test.txt','r')
                for line in f.readlines():
                	id,height,width,length=line.split()
                	print id,float(height)
                a 1.0
                b 3.0
                c 5.0
                Then in place of the print line a function could check whichever value you want and return the "type".

                Comment

                • jlm699
                  Contributor
                  • Jul 2007
                  • 314

                  #9
                  [CODE=python]
                  objs = []
                  objs.append(['a', 1.0, 2.5, 0.3]) # Invalid
                  objs.append(['b', 1.2, 5.0, 0.4]) # Type 1
                  objs.append(['c', 1.2, 4.0, 1.4]) # Type 2
                  objs.append(['d', 1.2, 4.5, 2.1]) # Invalid

                  for field in objs:
                  if (1.1 <= field[1] <= 2.1) and \
                  (3.2 <= field[2] <= 5.3) and \
                  (0.2 <= field[3] <= 0.5):
                  print field[0] + ' is type 1'
                  elif (1.0 <= field[1] <= 2.0) and \
                  (2.3 <= field[2] <= 4.2) and \
                  (1.2 <= field[3] <= 2.8):
                  print field[0] + ' is type 2'

                  b is type 1
                  c is type 2
                  [/CODE]

                  Comment

                  • bvdet
                    Recognized Expert Specialist
                    • Oct 2006
                    • 2851

                    #10
                    You could create a series of comparison lists mapped to the type:[code=Python]rangeDict = {'Type 1':([1,2],[3,4],[5,6]), 'Type 2': ([3,4],[5,6],[7,8]), 'Type 3': ([4,5],[6,7],[8,9])}
                    dataList = [[1.4,3.8,5.2], [4.1,6,9], [4,5.5,7.9], [3,4,6]]

                    resultList = []
                    for j, item in enumerate(dataL ist):
                    s = 'dataList item %d type is unknown' % j
                    for key in rangeDict:
                    for i, elem in enumerate(range Dict[key]):
                    comp_list = [elem[0] <= item[i] <= elem[1] for i, elem in enumerate(range Dict[key])]
                    if False not in comp_list:
                    s = 'dataList item %d is %s' % (j, key)
                    break
                    resultList.appe nd(s)

                    print '\n'.join(resul tList)[/code]
                    Output:

                    >>> dataList item 0 is Type 1
                    dataList item 1 is Type 3
                    dataList item 2 is Type 2
                    dataList item 3 type is unknown
                    >>>

                    Comment

                    • sippy99
                      New Member
                      • Feb 2008
                      • 7

                      #11
                      Originally posted by jlm699
                      [CODE=python]
                      objs = []
                      objs.append(['a', 1.0, 2.5, 0.3]) # Invalid
                      objs.append(['b', 1.2, 5.0, 0.4]) # Type 1
                      objs.append(['c', 1.2, 4.0, 1.4]) # Type 2
                      objs.append(['d', 1.2, 4.5, 2.1]) # Invalid

                      for field in objs:
                      if (1.1 <= field[1] <= 2.1) and \
                      (3.2 <= field[2] <= 5.3) and \
                      (0.2 <= field[3] <= 0.5):
                      print field[0] + ' is type 1'
                      elif (1.0 <= field[1] <= 2.0) and \
                      (2.3 <= field[2] <= 4.2) and \
                      (1.2 <= field[3] <= 2.8):
                      print field[0] + ' is type 2'

                      b is type 1
                      c is type 2
                      [/CODE]
                      Since I am a beginner, your code makes the most sense to me. But I am confused about the object.append in the beginning. What is exactly a, b, c, and d and where did it come from. Is a, for example an id, followed by height, length, range? But in my file (that the code runs through), I have thousand of ids with their corresponding info (height, length, width) and the info is in ranges (from x1 - x2) like in my parameters (Type 1, Type 2), not specific numbers. So would it be something like
                      Code:
                      Object.append (['a', 1.0-2.5 , 2.5-5 , .3 -.6])
                      Again, thanks to anyone that helps.

                      Comment

                      • jlm699
                        Contributor
                        • Jul 2007
                        • 314

                        #12
                        [CODE=python]
                        objs = []
                        # Elements of this list will be in the format:
                        # [ID, Height, Width, Depth]
                        objs.append(['a', 1.0, 2.5, 0.3])
                        objs.append(['b', 1.2, 5.0, 0.4])
                        objs.append(['c', 1.2, 4.0, 1.4])
                        objs.append(['d', 1.2, 4.5, 2.1])

                        for field in objs:
                        # This is the range for height (1.1 ~ 2.1)
                        if (1.1 <= field[1] <= 2.1) and \
                        # This is the range for width (3.2 ~ 5.3)
                        (3.2 <= field[2] <= 5.3) and \
                        # This is the range for depth (0.2 ~ 0.5)
                        (0.2 <= field[3] <= 0.5):
                        # If the input passes the above constraints it must by type 1
                        print field[0] + ' is type 1'
                        elif (1.0 <= field[1] <= 2.0) and \ # (1.0 ~ 2.0)
                        (2.3 <= field[2] <= 4.2) and \ # (2.3 ~ 4.2)
                        (1.2 <= field[3] <= 2.8): # (1.2 ~ 2.8)
                        # Passing the above constraints makes it type 2
                        print field[0] + ' is type 2'
                        [/CODE]

                        I hope that makes it a little bit clearer. I was just making the list of objects a random sample of possible inputs. The ranges that you described as type 1, type 2 are the constraints in the if, else if structure.

                        Comment

                        • sippy99
                          New Member
                          • Feb 2008
                          • 7

                          #13
                          Originally posted by jlm699
                          [CODE=python]
                          objs = []
                          # Elements of this list will be in the format:
                          # [ID, Height, Width, Depth]
                          objs.append(['a', 1.0, 2.5, 0.3])
                          objs.append(['b', 1.2, 5.0, 0.4])
                          objs.append(['c', 1.2, 4.0, 1.4])
                          objs.append(['d', 1.2, 4.5, 2.1])

                          for field in objs:
                          # This is the range for height (1.1 ~ 2.1)
                          if (1.1 <= field[1] <= 2.1) and \
                          # This is the range for width (3.2 ~ 5.3)
                          (3.2 <= field[2] <= 5.3) and \
                          # This is the range for depth (0.2 ~ 0.5)
                          (0.2 <= field[3] <= 0.5):
                          # If the input passes the above constraints it must by type 1
                          print field[0] + ' is type 1'
                          elif (1.0 <= field[1] <= 2.0) and \ # (1.0 ~ 2.0)
                          (2.3 <= field[2] <= 4.2) and \ # (2.3 ~ 4.2)
                          (1.2 <= field[3] <= 2.8): # (1.2 ~ 2.8)
                          # Passing the above constraints makes it type 2
                          print field[0] + ' is type 2'
                          [/CODE]

                          I hope that makes it a little bit clearer. I was just making the list of objects a random sample of possible inputs. The ranges that you described as type 1, type 2 are the constraints in the if, else if structure.
                          Thanks, this makes sense. Problem is nothing is outputting. I am using elif statements over many times, like following. I don't know if this is the problem or indentation, or something else.
                          [HTML]elif (5 <= Height <= 6) and \
                          (1 <= Length <= 2) and \
                          (9 <= Width <= 5):
                          print fields[0] + ' Type1'
                          elif (8 <= Height <= 9) and \
                          (2 <= Length <= 5) and \
                          (10 <= Width <= 12):
                          print fields[0] + ' Type2'
                          elif (11 <= Height <= 16) and \
                          (3 <= Length <= 4) and \
                          (11 <= Width <= 16):
                          print fields[0] + ' Type3'[/HTML]

                          Comment

                          • bvdet
                            Recognized Expert Specialist
                            • Oct 2006
                            • 2851

                            #14
                            Originally posted by sippy99
                            Thanks, this makes sense. Problem is nothing is outputting. I am using elif statements over many times, like following. I don't know if this is the problem or indentation, or something else.
                            [code=Python]elif (5 <= Height <= 6) and \
                            (1 <= Length <= 2) and \
                            (9 <= Width <= 5):
                            print fields[0] + ' Type1'
                            elif (8 <= Height <= 9) and \
                            (2 <= Length <= 5) and \
                            (10 <= Width <= 12):
                            print fields[0] + ' Type2'
                            elif (11 <= Height <= 16) and \
                            (3 <= Length <= 4) and \
                            (11 <= Width <= 16):
                            print fields[0] + ' Type3'[/code]
                            You can never have a 'Width' that is greater than 9 and less than 5. See 'Type1'. Please use code tags. Your indentation was messed up, but you would probably get an error message when you run the code. The code will work when corrected. Following is suggested code using a dictionary.[code=Python]Height = 5.5
                            Length = 1.5
                            Width = 8

                            rangeDict = {'Type 1':((5,6),(1,2) ,(5,9)),
                            'Type 2': ((8,9),(2,5),(1 0,12)),
                            'Type 3': ((11,16),(3,4), (11,16))
                            }
                            for key in rangeDict:
                            if False not in [a[0] <= b <= a[1] for a,b in zip(rangeDict[key], [Height, Length, Width])]:
                            print "Object %s" % key

                            >>> Object Type 1
                            >>> [/code]You should be able to tell I like dictionaries.

                            Comment

                            • raubana
                              New Member
                              • Feb 2008
                              • 55

                              #15
                              this is very simple. What you do is make a ' while loop', or a loop that contenues to work untill a condition is false. You make this loop change variables untill something is false, liek this:

                              Code:
                               
                              x = 150
                              #this is your variable. you can change it if you want to.
                              while x >100 and x < 200:
                                   # here you would put code that would change variables untill the 
                                   #condition "x>100 and x<200" is false
                              now a 'for loop' is used the same way as a while loop, but is made
                              specipicly for a list. it's like you take out some cards and place
                              them on a table. You take a pencil then and point at each card one at
                              a time. then you put the pencil away when your done.

                              here's an example:
                              Code:
                              x = [1,2,3]
                              #don't use ' x=(1,2,3) ' unless you don't want to change the list, 
                              #because a list with () instead of [] can't be changed.
                              for a in x:
                                   # 'a' can be changed, but you can't get the index (the spot in the list).
                                   # this will go through each part of the list.
                              you can also have list's inside lists, so you could make a for loop in a for
                              loop as well!

                              Code:
                              x=[[1,2,3],[4,5,6],[7,8,9]]
                              
                              for a in x:
                                  for b in a:
                                      #note: i used a different variable name for the objects in the list because
                                      # not doing so can mess up the loop!
                              ...and that's how you use a for loop and a while loop!

                              Comment

                              Working...