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


Groups > comp.lang.python > #10242

Re: Convert '165.0' to int

References <mailman.1315.1311240764.1164.python-list@python.org> <d62232ce-0600-48f6-8eaf-a2bda993d7c9@cq10g2000vbb.googlegroups.com> <j0ic3o$b5k$1@speranza.aioe.org>
Date 2011-07-25 15:46 +1000
Subject Re: Convert '165.0' to int
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1443.1311572784.1164.python-list@python.org> (permalink)

Show all headers | 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