python map function acting on a string of numbers
Solution 1
map()
may be somewhat faster than using list comprehension in some cases and in some cases map is slower than list comprehensions.
when using a built-in function:
python -mtimeit -s'xs=xrange(1000)' 'map(int,"1234567890")'
10000 loops, best of 3: 18.3 usec per loop
python -mtimeit -s'xs=xrange(1000)' '[int(x) for x in "1234567890"]'
100000 loops, best of 3: 20 usec per loop
with lambda
,map()
becomes slow:
python -mtimeit -s'xs=xrange(1000)' '[x*10 for x in "1234567890"]'
100000 loops, best of 3: 6.11 usec per loop
python -mtimeit -s'xs=xrange(1000)' 'map(lambda x:x*10,"1234567890")'
100000 loops, best of 3: 11.2 usec per loop
But, in python 3x map()
returns a map object, i.e. an iterator
Solution 2
Apply function to every item of iterable and return a list of the results.
From the documentation for map
int()
attempts to convert what is passed into an integer and will raise a ValueError
if you try something silly, like this:
>>> int('Hello')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'Hello'
map()
will return a list, which has the return value of the function that you ask it to call for any iterable. If your function returns nothing, then you'll get a list of None
s, like this:
>>> def silly(x):
... pass
...
>>> map(silly,'Hello')
[None, None, None, None, None]
It is the short and efficient way to do something like this:
def verbose_map(some_function,something):
results = []
for i in something:
results.append(some_function(i))
return results
acs
Updated on July 09, 2022Comments
-
acs almost 2 years
I've been playing around with the map function in Python and I was looking for some help in understanding the following behaviour:
foo="12345" print map(int,foo)
gives you
[1, 2, 3, 4, 5]
. Obviouslyint(foo)
spits out12345
. So what exactly is happening? Since strings are iterable by character, would the above two lines be synonymous withprint [int(x) for x in foo]
I know they will output the same result but is there anything different going on behind the scenes? Is one more efficient or better than another? Is one more "pythonic"?
Thanks a lot!
-
Perkins over 11 yearsI was refering to the list comprehension, map calls int repeatedly, so that one exists in both cases, the difference is that the iterable passed to map is passed with the cpython fast loop function, while the other one is parsed by a slower approach.
-
Perkins over 11 yearsIterating over a string of length 10e7 with [int(i) for i in s] takes ~5 seconds on my machine, map(int, s) takes 3.
-
Burhan Khalid over 11 yearsYou should clarify your answer then.
-
Perkins over 11 yearsOf course, is that better? I'm trying to figure out how to explain it better, preferably without dredging around through the C source code.
-
John La Rooy over 11 yearsYou seem to be a little confused.
map
isn't faster in this case because map is a builtin, it's because int is a builtin. The performance gain is so small for recent versions of Python that it's really not worth usingmap
for that reason alone -
Perkins over 11 yearsThe code uses int in both cases, the only difference is that one uses map, and the other uses a list comprehension. Even in versions other than CPython, there is almost certainly going to be a performance difference between the two, though which is faster is uncertain.