Using map (int, raw_input (). Split ())

Although I really like python when I need to get multiple integer inputs in one line, I prefer C / C ++. If I use python, I use:

a = map(int, raw_input().split()) 

Is this the only way or is there any pythonic way to do this? And is it worth how much time is considered?

+7
source share
3 answers

If you use a card with a built-in function, it may be slightly faster than the LC:

 >>> strs = " ".join(str(x) for x in xrange(10**5)) >>> %timeit [int(x) for x in strs.split()] 1 loops, best of 3: 111 ms per loop >>> %timeit map(int, strs.split()) 1 loops, best of 3: 105 ms per loop 

With custom function:

 >>> def func(x): ... return int(x) >>> %timeit map(func, strs.split()) 1 loops, best of 3: 129 ms per loop >>> %timeit [func(x) for x in strs.split()] 1 loops, best of 3: 128 ms per loop 

Python 3.3.1 comparison:

 >>> strs = " ".join([str(x) for x in range(10**5)]) >>> %timeit list(map(int, strs.split())) 10 loops, best of 3: 59 ms per loop >>> %timeit [int(x) for x in strs.split()] 10 loops, best of 3: 79.2 ms per loop >>> def func(x): return int(x) ... >>> %timeit list(map(func, strs.split())) 10 loops, best of 3: 94.6 ms per loop >>> %timeit [func(x) for x in strs.split()] 1 loops, best of 3: 92 ms per loop 

From Python Performance Tips :

The only limitation is that the "body of the loop" of the card must be a challenge function. Besides the syntactic benefits of understanding lists, they are often just as fast or faster than using the map equivalently.

+4
source

List understanding!

Intuitive and pythonic:

 a = [int(i) for i in raw_input().split()] 

Check out this discussion here: Python List Consrehension Vs. Map

+7
source

You can use this:

 s = raw_input().split() s = [int(i) for i in s] 
0
source

All Articles