newbie loop efficiency

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newmaid
    New Member
    • Feb 2008
    • 5

    newbie loop efficiency

    Hi all,

    I need some help understanding the efficiency of my code. I am new to python(and coding in general) :(

    if i have a code like this


    Code:
    L2 = []
    L3 = []
    
    i = 0
    while i < len(L0):
        if L1[i] == X:
            L2.append(Y)
            L3.append(Z)
        else:
            L2.append(0)
            L3.append(0)
        i = i + 1
    Is there a more efficient way to code this. (my code goes through a list and if something matches it appends values to other lists)


    Also


    Code:
    L2 = []
    
    i = 0
    while i < len(L0):
        j = 0
        while j < len(L1):
            if L0[i]  == L1[k]:
                L2.append(X)
                break
            else:
                L2.append(0)
           j = j + 1
        i = i + 1
    the second code compares 2 lists and creates a 3rd list based on the results.
    I am guessing this code is especially poor.

    your help is greatly appreciated.
    thanks for your time!
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    You will find from some of the sharpest guys here that you could probably do that in 1 line and about 10% of the characters. Let me just start with a more efficient way of looping over a list. This code just deals with the actual for loop mechanism, but notice there is no counter or conditional test. In plain english, for every value in the list do something, you don't necessarily have to reference the value inside the loop.
    Code:
    >>> L0=(1,2,3,4,5)
    >>> for value in L0:
    ... 	print value
    ... 	
    1
    2
    3
    4
    5
    >>> L0=(1,2,3)
    >>> L1=('a','b','c')
    >>> for number in L0:
    ... 	for letter in L1:
    ... 		print number,letter
    ... 		
    1 a
    1 b
    1 c
    2 a
    2 b
    2 c
    3 a
    3 b
    3 c
    If you want a simple counter copy the following into your interpreter
    Code:
     for i in range(5):print i
    and see how that works.

    Comment

    • newmaid
      New Member
      • Feb 2008
      • 5

      #3
      thanks for your reply .. i am looking forward to seeing some ubercode :)

      dshimer .. btw .. is

      Code:
      for i in range(5):print i
      faster than


      Code:
       
      i = 0
      while i < 5:
          print i
          i = i + 1

      Comment

      • diegososa
        New Member
        • Oct 2007
        • 19

        #4
        If you are worried about performance you should take a look at the module "timeit".
        Between the 2 previous codes (for & while), it's faster the while code.... but just a little, and makes the code ugliest.

        BTW, never bench functions with "print" statements, because it's too slow and make no sense.

        Comment

        • dshimer
          Recognized Expert New Member
          • Dec 2006
          • 136

          #5
          As far as cleaning up some of the other code. I would be interested in seeing where these values are coming from, how they are being stored, and what the goal is. It looks like you are reading through a sequence of coordinates and making decisions based on their relationship to other values. There may be code outside this loop which would influence the best way to accomplish the task.

          Comment

          Working...