Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: using split for a string : error

Reply
Thread Tools

Re: using split for a string : error

 
 
Chris Angelico
Guest
Posts: n/a
 
      01-24-2013
On Thu, Jan 24, 2013 at 9:37 PM, inshu chauhan <> 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[9] is equal to sp[10] and
> "Same class" when sp[9] is not equal to sp[10]. and sp[9] and sp[10] 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[9], sp[10]

Try this version:

print repr(sp[9]), repr(sp[10])

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[9] == sp[10]:

with this:

if int(sp[9]) == int(sp[10]):

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
needed.

ChrisA
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: using split for a string : error Tobias M. Python 0 01-24-2013 11:16 AM
Re: using split for a string : error Tobias M. Python 0 01-24-2013 10:55 AM
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
String#split(/\s+/) vs. String#split(/(\s+)/) Sam Kong Ruby 5 08-12-2006 07:59 PM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57