Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #25825
| References | <3rCdnUCiWpP1gZHNnZ2dnUVZ7vQAAAAA@giganews.com> <e9VOr.124006$PE.28478@fx04.am4> <500C2842.4040204@freenet.de> |
|---|---|
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | 2012-07-22 11:16 -0600 |
| Subject | Re: Converting a list of strings into a list of integers? |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2435.1342977424.4697.python-list@python.org> (permalink) |
On Sun, Jul 22, 2012 at 10:20 AM, Jan Riechers <janpeterr@freenet.de> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Converting a list of strings into a list of integers? Tony the Tiger <tony@tiger.invalid> - 2012-07-22 10:29 -0500
Re: Converting a list of strings into a list of integers? Roy Smith <roy@panix.com> - 2012-07-22 11:39 -0400
Re: Converting a list of strings into a list of integers? Tony the Tiger <tony@tiger.invalid> - 2012-07-22 11:01 -0500
Re: Converting a list of strings into a list of integers? Peter Otten <__peter__@web.de> - 2012-07-22 18:30 +0200
Re: Converting a list of strings into a list of integers? Alister <alister.ware@ntlworld.com> - 2012-07-22 15:39 +0000
Re: Converting a list of strings into a list of integers? Tony the Tiger <tony@tiger.invalid> - 2012-07-22 11:01 -0500
Re: Converting a list of strings into a list of integers? Jan Riechers <janpeterr@freenet.de> - 2012-07-22 19:20 +0300
Re: Converting a list of strings into a list of integers? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-22 17:01 +0000
Re: Converting a list of strings into a list of integers? Jan Riechers <janpeterr@freenet.de> - 2012-07-22 20:27 +0300
Re: Converting a list of strings into a list of integers? Grant Edwards <invalid@invalid.invalid> - 2012-07-23 14:27 +0000
Re: Converting a list of strings into a list of integers? rusi <rustompmody@gmail.com> - 2012-07-23 08:31 -0700
Re: Converting a list of strings into a list of integers? David Robinow <drobinow@gmail.com> - 2012-07-22 13:03 -0400
Re: Converting a list of strings into a list of integers? Jan Riechers <janpeterr@freenet.de> - 2012-07-22 20:10 +0300
Re: Converting a list of strings into a list of integers? Ian Kelly <ian.g.kelly@gmail.com> - 2012-07-22 11:16 -0600
Re: Converting a list of strings into a list of integers? Paul Rubin <no.email@nospam.invalid> - 2012-07-22 10:03 -0700
Re: Converting a list of strings into a list of integers? Dave Angel <d@davea.name> - 2012-07-22 21:03 -0400
csiph-web