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


Groups > comp.lang.python > #83566

Re: extracting numbers with decimal places from a string

Path csiph.com!usenet.pasdenom.info!news.franciliens.net!news.muarf.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <joel.goldstick@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'float': 0.07; 'modify': 0.07; 'string': 0.09; 'line:': 0.09; 'strings.': 0.09; 'subject:string': 0.09; 'worked.': 0.09; 'cc:addr:python- list': 0.11; 'jan': 0.12; 'question.': 0.14; "','": 0.16; '4.4,': 0.16; 'commas:': 0.16; 'did:': 0.16; 'separated': 0.16; 'wanted.': 0.16; 'wrote:': 0.18; 'split': 0.19; 'cc:addr:python.org': 0.22; 'print': 0.22; 'question': 0.24; 'cc:2**0': 0.24; 'script': 0.25; 'values': 0.27; 'header:In-Reply-To:1': 0.27; '----': 0.29; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; "skip:' 10": 0.31; 'decimal': 0.31; 'fine,': 0.31; 'subject:numbers': 0.31; 'url:python': 0.33; '-----': 0.33; 'subject:from': 0.34; 'problem': 0.35; 'subject:with': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'january': 0.37; 'list': 0.37; 'starting': 0.37; 'thank': 0.38; 'pm,': 0.38; 'url:mail': 0.40; 'how': 0.40; 'solve': 0.60; 'new': 0.61; 'numbers': 0.61; 'first': 0.61; 'total': 0.65; 'different': 0.65; 'to:addr:gmail.com': 0.65; 'here': 0.66; '2015': 0.84; 'joel': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=hEFp6fLx46PMG9NolfdV1q+ZEn0l/ZFxExdx8WDc+TQ=; b=iaSuMb+c4DlvmQooqJtyB878OjycPaClK+6iSDzjpAQrgHzloo5ZnudSGEuUx9hCea we0FLOaXMh/si3znDppJRPnvWDec2ngSCbkCCSmGqp3T5EB1Oil72siHavIz3QMhKP1v 0jw+ABWwzM2reBIeZ6YDNI9CK95EDZH7uLxBTlUg+aJummn9Hh9kTUlx/wQ4aK3+1pao mHShC4u57Osa51e5zyEhc5XZ+K0Jfx45CLo1sAYTTXPsTnE19GMLpN4Iej3/DrHflh4o 5PDCviTRR1CO6rhqCQOf3i+abcumH2VviiWgVrbTCfSbelDayujFUJwKy7cfbLcymtsC gW/A==
MIME-Version 1.0
X-Received by 10.140.20.226 with SMTP id 89mr43002349qgj.43.1421012577248; Sun, 11 Jan 2015 13:42:57 -0800 (PST)
In-Reply-To <3bfeea22-cded-4363-92fa-6f1b33ddae16@googlegroups.com>
References <7240a574-dfd7-46c3-80eb-0657b41894bb@googlegroups.com> <mailman.17591.1421006803.18130.python-list@python.org> <3bfeea22-cded-4363-92fa-6f1b33ddae16@googlegroups.com>
Date Sun, 11 Jan 2015 16:42:57 -0500
Subject Re: extracting numbers with decimal places from a string
From Joel Goldstick <joel.goldstick@gmail.com>
To Store Makhzan <store0a@gmail.com>
Content-Type text/plain; charset=UTF-8
Cc "python-list@python.org" <python-list@python.org>
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 <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.17595.1421012580.18130.python-list@python.org> (permalink)
Lines 62
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1421012580 news.xs4all.nl 2930 [2001:888:2000:d::a6]:53629
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:83566

Show key headers only | View raw


On Sun, Jan 11, 2015 at 3:26 PM, Store Makhzan <store0a@gmail.com> wrote:
> On Sunday, January 11, 2015 at 2:06:54 PM UTC-6, Joel Goldstick wrote:
>> On Sun, Jan 11, 2015 at 2:31 PM, Store Makhzan wrote:
>> > I have this script which can calculate the total of numbers given in a string
>> > ---- script -----
>> > total = 0
>> > for c in '0123456789':
>> >    total += int(c)
>> > print total
>> > ---- script -----
>> >
>> > How should I modify this script to find the total of if the numbers given in the string form have decimal places? That is, how do I need to modify this line:
>> > ----- script -----
>> > for c in '1.32, 5.32, 4.4, 3.78':
>> > ----- script -----
>> > to find the total of these given numbers.
>> > --
>> > https://mail.python.org/mailman/listinfo/python-list
>>
>> split the string on ',' to get a list of strings.  loop thru list
>> using float(s), add float values
>>
>>
>> --
>> Joel Goldstick
>> http://joelgoldstick.com
>
> Thank you
> Here is what I did:
>
> ----- script -----
> #Check if a perfect cube
> total = 0
> for c in ('1.23', '2.4', '3.123'):
>    print float(c)
>    total += float(c)
> print total
> ----- script -----
>
> Which gave me the result I wanted.
> Since I am new to Python, I used () as a guess, and it worked.

That's fine, but its different than your original question.  In your
original question you had a string of floats separated by commas.  To
solve that problem you need to first split the string on the commas:

my_list = "1.23, 2.4, 3.123".split(",")

that will give you ['1.23', '2.4', '3.123']

>From there do what you did starting with your for loop



> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com

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


Thread

extracting numbers with decimal places from a string Store Makhzan <store0a@gmail.com> - 2015-01-11 11:31 -0800
  Re: extracting numbers with decimal places from a string Joel Goldstick <joel.goldstick@gmail.com> - 2015-01-11 15:06 -0500
    Re: extracting numbers with decimal places from a string Store Makhzan <store0a@gmail.com> - 2015-01-11 12:26 -0800
      Re: extracting numbers with decimal places from a string Joel Goldstick <joel.goldstick@gmail.com> - 2015-01-11 16:42 -0500
        Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-11 23:20 +0100
          Re: extracting numbers with decimal places from a string Joel Goldstick <joel.goldstick@gmail.com> - 2015-01-11 17:54 -0500
            Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 00:12 +0100
              Re: extracting numbers with decimal places from a string Joel Goldstick <joel.goldstick@gmail.com> - 2015-01-11 18:35 -0500
                Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 00:46 +0100
                Re: extracting numbers with decimal places from a string Peter Otten <__peter__@web.de> - 2015-01-12 01:09 +0100
                Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 01:45 +0100
                Re: extracting numbers with decimal places from a string Chris Angelico <rosuav@gmail.com> - 2015-01-12 11:17 +1100
          Re: extracting numbers with decimal places from a string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-12 11:45 +1100
        Re: extracting numbers with decimal places from a string Grant Edwards <invalid@invalid.invalid> - 2015-01-11 23:06 +0000
  Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 00:07 +0100
    Re: extracting numbers with decimal places from a string Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-12 00:04 +0000
      Re: extracting numbers with decimal places from a string Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-01-12 02:00 +0100
    Re: extracting numbers with decimal places from a string MRAB <python@mrabarnett.plus.com> - 2015-01-12 00:59 +0000
    Re: extracting numbers with decimal places from a string Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-01-12 12:13 +1100

csiph-web