Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Converting a list of strings into a list of integers?

Reply
Thread Tools

Re: Converting a list of strings into a list of integers?

 
 
Ian Kelly
Guest
Posts: n/a
 
      07-22-2012
On Sun, Jul 22, 2012 at 10:20 AM, Jan Riechers <(E-Mail Removed)> wrote:
> Hi,
>
> I am not sure why everyone is using the for-iterator option over a "map",
> but I would do it like that:
>
> MODUS_LIST= map(int, options.modus_list)
>
> "map" works on a list and does commandX (here "int" conversion, use "str"
> for string.. et cetera) on sequenceY, returning a sequence. More in the help
> file.
>
> And if I'm not completely mistaken, it's also the quicker way to do
> performance wise. But I can't completely recall the exact reason.


My recollection is that map has the edge if you can pass it a built-in
or a C extension function, like int, or a complicated Python function
that you would end up calling anyway in the list comprehension. The
situation changes though if you can write the comprehension to remove
the overhead of the Python function call. For example:

map(lambda x: x+1, my_list)

[x+1 for x in my_list]

By performing the addition inline instead of calling a function to do
it, the list comprehension wins performance-wise in this scenario. So
as a simple rule of thumb I will typically choose between map or a
comprehension based on whether I need to call a function or not (and
also based on how pretty or ugly the resulting code is). Anything
further would just be premature optimization. Also keep in mind that
in Python 3 map returns an iterator instead of a list, so for a fair
comparison you would have to compose the map with a list() call.

Cheers,
Ian
 
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: Converting a list of strings into a list of integers? Jan Riechers Python 4 07-23-2012 03:31 PM
Re: Converting a list of strings into a list of integers? Jan Riechers Python 0 07-22-2012 05:10 PM
Re: Converting a list of strings into a list of integers? David Robinow Python 0 07-22-2012 05:03 PM
Re: Converting a list of strings into a list of integers? Paul Rubin Python 0 07-22-2012 05:03 PM
Re: Converting a list of strings into a list of integers? Roy Smith Python 1 07-22-2012 04:30 PM



Advertisments