By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,067 Members | 1,818 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,067 IT Pros & Developers. It's quick & easy.

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

P: 55
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:

Expand|Select|Wrap|Line Numbers
  1.     if(direction == "forward"):
  2.     counter = 0
  3.     for i in range(self.pos_indice,len(self.indice)):
  4.         if(self.indice[i+1].tagName == "h1"):
  5.            break
  6.         else:
  7.            counter = counter+1
  8.     return counter+1
  9.  
  10.  

but in reverse my code fails as shown below:

Expand|Select|Wrap|Line Numbers
  1.  
  2. if(direction == "backword"):
  3.     rev_counter = self.pos_indice
  4.     for j in range(self.pos_indice,0,-1):
  5.         if(self.indice[j+1].tagName == "h1"):
  6.            break
  7.         else:
  8.            rev_counter = rev_counter + 1
  9.     return rev_counter+1
  10.  
Please advise with a solution fast

Thanks
Anes
Jan 25 '16 #1
Share this Question
Share on Google+
2 Replies


100+
P: 299
Python has a built-in reversed() function which can be used in a For loop.
Jan 25 '16 #2

P: 55
Dear Luk3r,
I used following code for purpose and feel works fine 90% !!

Expand|Select|Wrap|Line Numbers
  1.  
  2.     if(direction == "backword"):
  3.     rev_counter = 0
  4.     for j in range(self.pos_indice,-1,-1):
  5.  
  6.         if(self.indice[j-1].tagName == "h1"):
  7.            break
  8.         else:
  9.            rev_counter = rev_counter + 1
  10.     return rev_counter+1
  11.  
Please advise any further modifications....

Thanks
Anes
Jan 26 '16 #3

Post your reply

Sign in to post your reply or Sign up for a free account.