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


Groups > comp.lang.python > #10242

Re: Convert '165.0' to int

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'integers': 0.09; 'it;': 0.09; 'iterate': 0.09; '25,': 0.12; 'am,': 0.13; 'wrote:': 0.15; '(eg': 0.16; 'billy': 0.16; 'evaluates': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'sequence,': 0.16; 'subject:Convert': 0.16; 'suggested.': 0.16; "subject:' ": 0.16; 'mon,': 0.16; 'received:209.85.210.174': 0.19; 'received:mail- iy0-f174.google.com': 0.19; 'memory': 0.21; 'variable': 0.21; 'header:In-Reply-To:1': 0.22; 'personally,': 0.23; 'creating': 0.24; 'index': 0.25; 'function': 0.26; '(and': 0.27; 'message- id:@mail.gmail.com': 0.28; 'actually': 0.33; 'list.': 0.33; 'to:addr:python-list': 0.34; 'however,': 0.34; "can't": 0.34; 'that,': 0.35; 'beginning': 0.36; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'should': 0.39; 'list,': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.40; "i'd": 0.40; 'tiny': 0.68; 'speed,': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=WXsv+KKNCVHsPQL0D9MAc2d+stWsyWNIRaLfo1o+u8Y=; b=NgIcp3Piab6Lm4KlZ+8Fgqwgt5Zqz4mCK5xvRvxNc1s3Hmu6JxKVZSr/e6ojz8op3J FyVcEGmBvztFXNjfGEgc+kdirUfGTUqls96Ke+YI8m8Hor+auS/Yzrx5CVx8zmr2JfKa 8n88bwjErRAVFPRs1gY7oC2j0AOyg+ptAdTxU=
MIME-Version 1.0
In-Reply-To <j0ic3o$b5k$1@speranza.aioe.org>
References <mailman.1315.1311240764.1164.python-list@python.org> <d62232ce-0600-48f6-8eaf-a2bda993d7c9@cq10g2000vbb.googlegroups.com> <j0ic3o$b5k$1@speranza.aioe.org>
Date Mon, 25 Jul 2011 15:46:22 +1000
Subject Re: Convert '165.0' to int
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.12
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.1443.1311572784.1164.python-list@python.org> (permalink)
Lines 17
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1311572784 news.xs4all.nl 23891 [2001:888:2000:d::a6]:54024
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:10242

Show key headers only | View raw


On Mon, Jul 25, 2011 at 10:07 AM, Billy Mays <noway@nohow.com> wrote:
> if the goal is speed, then you should use generator expressions:
>
> list_of_integers = (int(float(s)) for s in list_of_strings)

Clarification: This is faster if and only if you don't actually need
it as a list. In spite of the variable name, it's NOT a list, and you
can't index it (eg you can't work with list_of_integers[7]). However,
you can iterate over it to work with the integers in sequence, and for
that specific (and very common) use, it will be faster and use less
memory than actually creating the list. It's also going to be a LOT
faster than creating the list, if you only need a few from the
beginning of it; the generator evaluates lazily.

Personally, I'd just create a tiny function and use that, as has been suggested.

ChrisA

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


Thread

Convert '165.0' to int "Frank Millman" <frank@chagford.com> - 2011-07-21 11:31 +0200
  Re: Convert '165.0' to int SigmundV <sigmundv@gmail.com> - 2011-07-24 11:27 -0700
    Re: Convert '165.0' to int Billy Mays <noway@nohow.com> - 2011-07-24 20:07 -0400
      Re: Convert '165.0' to int Chris Angelico <rosuav@gmail.com> - 2011-07-25 15:46 +1000
      Re: Convert '165.0' to int Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-25 19:48 +1000
        Re: Convert '165.0' to int SigmundV <sigmundv@gmail.com> - 2011-07-25 09:39 -0700
        Re: Convert '165.0' to int Billy Mays <81282ed9a88799d21e77957df2d84bd6514d9af6@myhashismyemail.com> - 2011-07-25 13:11 -0400

csiph-web