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:
someList=['<head>','</head>]
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.
How to traverse strings, in a list index.
Page 1 of 11 Replies - 113 Views - Last Post: 02 February 2013 - 04:07 PM
Replies To: How to traverse strings, in a list index.
#2
Re: How to traverse strings, in a list index.
Posted 02 February 2013 - 04:07 PM
Something to play with here...
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.
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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|