1 Replies - 1383 Views - Last Post: 02 February 2013 - 04:07 PM Rate Topic: -----

#1 Heiland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-January 13

How to traverse strings, in a list index.

Posted 02 February 2013 - 03:04 PM

I am writing a program to check the opening and closeing tags of an html document. To do this I am putting string vertions of the tags in a list and comparing the last index of the list to the second-to-last index of the list. What I want to do is, if the letters of index[-2] from 2:-1 are the same as all of the letters in index-1 from , I want to remove both of them from the list. My problem is that I dont know how to compare specific letter of a string within a list, to the letters of a different string within that same list. In pseudocode what I am trying to do may look something like this:

if someList[-2,2:-1] in someList[-1,1:-1]: # the 2:-1 & 1:-1 referencing the letters of the string in index -2 and -1
remove someList[-1] and someList[-2]

I would be happy to answer any questions if the description is not clear enough.

Is This A Good Question/Topic? 0
  • +

Replies To: How to traverse strings, in a list index.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5187
  • View blog
  • Posts: 13,917
  • Joined: 18-April 07

Re: How to traverse strings, in a list index.

Posted 02 February 2013 - 04:07 PM

Something to play with here...

somelist = ['<head>','</head>']

print(somelist[0][-2:]) # Prints 'd>' the last two characters of the first element in the list

Notice we are taking one of the strings in our list and then slicing it. This should then allow you to compare characters of any two strings in your list.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1