working with arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Safy
    New Member
    • Mar 2007
    • 4

    working with arrays

    Hello, i really need some help, im new to python so having a bit of trouble.
    i have two arrays which i successfully pulled out from some data. i want to search these arrays for 2 number and when they appear i want their index.
    Code:
    >>> arrF = [3,4,5,6,7,8,9]
    >>> arrid=[0,0,0,1,0,0,0]
    >>> def indexOfObjectinFrame2(frame,arrF,arrID,id):
    ...  for i in arrF:
    ...    if arrF[i] == frame:
    ...      if arrid [i] == id:
    ...        print i
    ...      else:
    ...        print -1
    no matter how many different ways i try this, it just wont work, i keep getting errors. why does python not like this?
    Last edited by bartonc; Mar 5 '07, 04:22 PM. Reason: added [code][/code] tags
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by Safy
    Hello, i really need some help, im new to python so having a bit of trouble.
    i have two arrays which i successfully pulled out from some data. i want to search these arrays for 2 number and when they appear i want their index.

    >>> arrF = [3,4,5,6,7,8,9]
    >>> arrid=[0,0,0,1,0,0,0]
    >>> def indexOfObjectin Frame2(frame,ar rF,arrID,id):
    ... for i in arrF:
    ... if arrF[i] == frame:
    ... if arrid [i] == id:
    ... print i
    ... else:
    ... print -1
    no matter how many different ways i try this, it just wont work, i keep getting errors. why does python not like this?
    its easy to "search" for array elements. use the "in" keyword (if you don't want to know the position.
    eg
    Code:
    ....
    if frame in arrF and id in arrid:
       ....
    another way,
    Code:
    ...
    for i in range(len(arrF)):
       if frame == arrF[i] and id == arrid[i]:
          .....

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by Safy
      Hello, i really need some help, im new to python so having a bit of trouble.
      i have two arrays which i successfully pulled out from some data. i want to search these arrays for 2 number and when they appear i want their index.
      Code:
      >>> arrF = [3,4,5,6,7,8,9]
      >>> arrid=[0,0,0,1,0,0,0]
      >>> def indexOfObjectinFrame2(frame,arrF,arrID,id):
      ...  for i in arrF:
      ...    if arrF[i] == frame:
      ...      if arrid [i] == id:
      ...        print i
      ...      else:
      ...        print -1
      no matter how many different ways i try this, it just wont work, i keep getting errors. why does python not like this?
      While my friend ghostdog74 does show a better way (there are several) to get the index of an object in a LIST (arrays are a slightly different animal), I found your problem:
      Code:
      def indexOfObjectinFrame2(frame,arrF,arrID,id):
      ##    for i in arrF: This is 3,4,5,6,7,8,9
          for i in range(len(arrF)):
              if arrF[i] == frame:
                  if arrid[i] == id:
                      print i
                  else:
                      print -1
      
      arrF = [3,4,5,6,7,8,9]
      arrid=[0,0,0,1,0,0,0]
      
      indexOfObjectinFrame2(6, arrF, arrid, 1)
      I have used code tags and run this in a module so that you may copy and paste it into you module. If you do this, you won't have to retype the whole thing to make changes. Welcome to TheScripts.

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by bartonc
        While my friend ghostdog74 does show a better way (there are several) to get the index of an object in a LIST (arrays are a slightly different animal), I found your problem:
        Code:
        def indexOfObjectinFrame2(frame,arrF,arrID,id):
        ##    for i in arrF: This is 3,4,5,6,7,8,9
            for i in range(len(arrF)):
                if arrF[i] == frame:
                    if arrid[i] == id:
                        print i
                    else:
                        print -1
        
        arrF = [3,4,5,6,7,8,9]
        arrid=[0,0,0,1,0,0,0]
        
        indexOfObjectinFrame2(6, arrF, arrid, 1)
        I have used code tags and run this in a module so that you may copy and paste it into you module. If you do this, you won't have to retype the whole thing to make changes. Welcome to TheScripts.
        Or:
        Code:
        def indexOfObjectinFrame2(frame,arrF,arrID,id):
            for i, item in enumerate(arrF):     # This is [0,1,2...], [3,4,5...]
                if item == frame:
                    if arrid[i] == id:
                        print i
                    else:
                        print -1
        
        arrF = [3,4,5,6,7,8,9]
        arrid=[0,0,0,1,0,0,0]
        
        indexOfObjectinFrame2(6, arrF, arrid, 1)

        Comment

        • Safy
          New Member
          • Mar 2007
          • 4

          #5
          Wow thanks...i didnt think anyone would actually reply..This forum is great...thankks again guys..
          Safy

          Comment

          • Safy
            New Member
            • Mar 2007
            • 4

            #6
            okaay that worked fine, when i call this with another function it returns 'None'.
            The data i am using is actually really large so i would like to call pass it numbers automatically, the code below looks fine to me ..why does is it not working?
            just to refresh:
            I have a series of arrays, I want to find the index of object '0' in frame F from the data.
            When i have the index for the object, if that object exists in that
            frame, i will analyse the data (some maths).(hope it makes sense)


            Code:
            arrayX = [190,193,197,198,201,201,201,202]
            arrayY = [125,125,124,126,125,126,127,126]
            arrF = [289,290,291,292,293,294,295,296]
            arrid = [0,0,0,0,1,0,0,0]
            
            def indexOfObjectinFrame(frame,arrF,arrID,id):
                for i in range(len(arrF)):
                    if arrF[i] == frame:
                        if arrid[i] == id:
                            print i
                        else:
                            print -1000
            
            def compareObjects(GTX,GTY,GTF,GTid,gtobjid):
              totalDistance = 0
              i = 0 
              while i <= 9: 
                print i                               #largest frame number in either file
                GTindex = indexOfObjectinFrame(i,GTF,GTid,gtobjid) 
                print "GTINDEX IS ", GTindex
               
                if GTindex >= 0: 
                 print "index found"#, Total_distance here
                i = i+1 
            
            
            # call above
            C = compareObjects(arrX,arrY,arrF,arrid,0)  #...GT number here...,.tracker number here...)
            Why does this say 'None' when executed?
            Thanks,S

            Comment

            • bvdet
              Recognized Expert Specialist
              • Oct 2006
              • 2851

              #7
              Originally posted by Safy
              okaay that worked fine, when i call this with another function it returns 'None'.
              The data i am using is actually really large so i would like to call pass it numbers automatically, the code below looks fine to me ..why does is it not working?
              just to refresh:
              I have a series of arrays, I want to find the index of object '0' in frame F from the data.
              When i have the index for the object, if that object exists in that
              frame, i will analyse the data (some maths).(hope it makes sense)


              Code:
              arrayX = [190,193,197,198,201,201,201,202]
              arrayY = [125,125,124,126,125,126,127,126]
              arrF = [289,290,291,292,293,294,295,296]
              arrid = [0,0,0,0,1,0,0,0]
              
              def indexOfObjectinFrame(frame,arrF,arrID,id):
                  for i in range(len(arrF)):
                      if arrF[i] == frame:
                          if arrid[i] == id:
                              print i
                          else:
                              print -1000
              
              def compareObjects(GTX,GTY,GTF,GTid,gtobjid):
                totalDistance = 0
                i = 0 
                while i <= 9: 
                  print i                               #largest frame number in either file
                  GTindex = indexOfObjectinFrame(i,GTF,GTid,gtobjid) 
                  print "GTINDEX IS ", GTindex
                 
                  if GTindex >= 0: 
                   print "index found"#, Total_distance here
                  i = i+1 
              
              
              # call above
              C = compareObjects(arrX,arrY,arrF,arrid,0)  #...GT number here...,.tracker number here...)
              Why does this say 'None' when executed?
              Thanks,S
              Use the 'return' statement to return a value. 'print' always returns 'None'. Here is a simple example:
              Code:
              def is_prime(n):
                  count = 2
                  while count < n**0.5:
                      if n % count == 0:
                          print '%d is a product of %d and %d' % (n, count, n/count)
                          return False
                      count += 1
                  print n, 'is a prime number'
                  return True
              Notice there are two 'return' statements. If a condition is met, one value is returned. If not, further processing takes place and returns a different value.

              Comment

              • Safy
                New Member
                • Mar 2007
                • 4

                #8
                Ahah..i got it..well i think this is right anyway.
                in the code i included print statements everywhere hoping it will help me debug the code BUT as i already have print statements int he functions it was causing problems, so after removing them and sorting out 'global name errors' i have some index numbers :)

                yaaay im not a programmer but i will get there :)
                S

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by Safy
                  Wow thanks...i didnt think anyone would actually reply..This forum is great...thankks again guys..
                  Safy
                  Glad you like it. Stick around. You'll learn tons.

                  Comment

                  Working...