How to reverse a list for the purpose of finding the previous item with a condition ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amskape
    New Member
    • Apr 2010
    • 56

    How to reverse a list for the purpose of finding the previous item with a condition ?

    Dear friends,
    I try to find the forward and backward content of a play list . The main item is identified using <h1> tag ...
    I wrote a code for forward item which works fine as below:

    Code:
        if(direction == "forward"):
    	counter = 0
    	for i in range(self.pos_indice,len(self.indice)):
    	    if(self.indice[i+1].tagName == "h1"):
    	       break
    	    else:
    	       counter = counter+1
    	return counter+1

    but in reverse my code fails as shown below:

    Code:
    if(direction == "backword"):
    	rev_counter = self.pos_indice
    	for j in range(self.pos_indice,0,-1):
    	    if(self.indice[j+1].tagName == "h1"):
    	       break
    	    else:
    	       rev_counter = rev_counter + 1
    	return rev_counter+1
    Please advise with a solution fast

    Thanks
    Anes
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    Python has a built-in reversed() function which can be used in a For loop.

    Comment

    • amskape
      New Member
      • Apr 2010
      • 56

      #3
      Dear Luk3r,
      I used following code for purpose and feel works fine 90% !!

      Code:
          if(direction == "backword"):
      	rev_counter = 0
      	for j in range(self.pos_indice,-1,-1):
      	    
      	    if(self.indice[j-1].tagName == "h1"):
      	       break
      	    else:
      	       rev_counter = rev_counter + 1
      	return rev_counter+1
      Please advise any further modifications.. ..

      Thanks
      Anes

      Comment

      Working...