Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37558

Re: using split for a string : error

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.04; 'strings.': 0.07; 'python': 0.09; '#print': 0.09; 'correct,': 0.09; 'currently,': 0.09; 'if,': 0.09; 'integers': 0.09; 'parsed': 0.09; 'subject:string': 0.09; 'subject:using': 0.09; 'subject:error': 0.11; '24,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'integers,': 0.16; 'repr()': 0.16; 'values:': 0.16; 'wrote:': 0.17; 'instance,': 0.17; 'integer': 0.17; 'thu,': 0.17; 'jan': 0.18; '(or': 0.18; 'supposed': 0.21; 'strip': 0.22; 'needed.': 0.23; 'this:': 0.23; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'values': 0.26; 'replace': 0.27; 'message-id:@mail.gmail.com': 0.27; 'behaving': 0.29; "they'll": 0.29; 'convert': 0.29; 'subject: : ': 0.30; 'file': 0.32; 'print': 0.32; 'version:': 0.33; 'to:addr:python- list': 0.33; 'equal': 0.33; "can't": 0.34; 'received:google.com': 0.34; 'pm,': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'why': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'things': 0.38; 'to:addr:python.org': 0.39; 'little': 0.39; 'think': 0.40; 'your': 0.60; 'skip:u 10': 0.60; "you've": 0.61; 'different': 0.63; 'show': 0.63; 'results': 0.65; 'programme': 0.69; 'obvious': 0.71; '2013': 0.84; 'advise,': 0.84; 'same,': 0.91; 'try.': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=ZcMBeH+m27kQqYjnaJxhtlEjqy1RCWaVeymPxD+Z4G4=; b=ub7hvEohZXoh4kCB4EaXBxh0yAKtkUw4fahBhTCUS8yAL0Kw0AZSME4e9U/n++iRzK tnBYy5W1hOVT2o0c8hPss1cwOjEo4IPyTnBpSAt2aFNhqnHGyaKtijsb3jhkqSwQjEBe 20oqjnqLChZRZIg9lyVWT1Xc78v6xT6NjlJ2moai1xyRi0If7VkszxFlvMkgcSE+u4WA aA5BejcoqTPS1LHgPKkuwUSN81QBqX9ObD83uoxalHP7SsAAcgSKmO8yGi4Tkc12ywEC Gh0ygFrPKjDmXw4wq7wscqJXJt3DR8eAq9D7hP8ZTrNNWep5+HD6mI8dvUOK2azx4lXv Z3Dg==
MIME-Version 1.0
X-Received by 10.68.189.163 with SMTP id gj3mr3645212pbc.110.1359025115505; Thu, 24 Jan 2013 02:58:35 -0800 (PST)
In-Reply-To <CAFqGZRGMXcku_nM_LqPsHpCzqGxF57LOSKwKwoCH+LyhPA8t_A@mail.gmail.com>
References <CAFqGZRGMXcku_nM_LqPsHpCzqGxF57LOSKwKwoCH+LyhPA8t_A@mail.gmail.com>
Date Thu, 24 Jan 2013 21:58:35 +1100
Subject Re: using split for a string : error
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.958.1359025124.2939.python-list@python.org> (permalink)
Lines 37
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1359025124 news.xs4all.nl 6909 [2001:888:2000:d::a6]:60833
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:37558

Show key headers only | View raw


On Thu, Jan 24, 2013 at 9:37 PM, inshu chauhan <insideshoes@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[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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: using split for a string : error Chris Angelico <rosuav@gmail.com> - 2013-01-24 21:58 +1100

csiph-web