using split for a string : error
rosuav at gmail.com
Thu Jan 24 11:58:35 CET 2013
On Thu, Jan 24, 2013 at 9:37 PM, inshu chauhan <insideshoes at gmail.com> wrote:
> For me I think the programme is logically correct, but its giving me results
> which are strange.
> It is Printing " Different Class" even when sp is equal to sp and
> "Same class" when sp is not equal to sp. and sp and sp are
> simple integers like 3, 3, 4 ,4.
> I have a little understanding why the programme is behaving like this ?
Without your data file I can't advise, but here's a couple of things
to try. I see you've tried displaying the values:
#print sp, sp
Try this version:
print repr(sp), repr(sp)
That'll make it obvious if, for instance, there are leading/trailing spaces.
The other thing you may want to consider, if the values are supposed
to be integers, is to convert them to Python integers before
comparing. Currently, you're working with strings. Replace this:
if sp == sp:
if int(sp) == int(sp):
That will consider "1" and "1 " to be the same, since they'll both be
parsed as the integer 1. Alternatively, consider what Tobias said and
explicitly strip spaces. Either way, displaying repr() of the strings
(or printing the whole of sp, as Tobias suggests) will show you what's
More information about the Python-list