Anurag Joshi Anurag Joshi - 17 days ago 5
Python Question

Compare non identical objects with same value

I have 2 lists,both in different formats but same content.
For example, doing a simple print command for the 1st elements of the list does the following:

prefix_txt[0]=SEF00
prefix_confluence[0]=[u'SEF00']


I get the 'u' here is due to the encoding..

The prefix_confluence is being parsed by a HTML parser this way:

soup=BeautifulSoup(sample,'html.parser')
for row in soup.find_all('tr')[2:171]:
prefix_confluence.append(row.get_text(strip=True, separator='|').split('|')[0:1])


Now, how do I compare and show that these 2 list elements are in fact equal in content?
I have tried this:

new=str(prefix_confluence[0]).strip('[u'']')
if(prefix_txt[0]==new):
print "They are same."


But they dont display the print message due to obvious reasons.
How can I make them equal? I also tried join, concatenation but was not able to make it work.

Answer

If prefix_txt[0] is a string 'SEF00', and prefix_confluence[0] is a list containing a unicode version of that same string [u'SEF00'], then you should be able to do the following:

new = prefix_confluence[0][0]
if(prefix_txt[0] == new):
        print "They are same."
Comments