using map(int, raw_input().split())
Asked Answered
M

4

10

Though I like python very much, When I need to get multiple integer inputs in the same 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 it? And does this cost much as far as time is considered?

Mahaffey answered 15/6, 2013 at 8:35 Comment(4)
You don't get the conversions in C/C++ for free either.Humanity
No but C/C++ can recognize multiple integers when they are seperated by space. So, I can get them as multiple integers directly.Mahaffey
It doesn't "recognize" them, it has to parse the string.Humanity
The difference is, I have to take the whole line of integers as a single line(String) in python. But i can take it as an array of integers in CMahaffey
R
6

If you're using map with built-in function then it can be slightly faster than 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 user-defined 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 comparisons:

>>> 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 page:

The only restriction is that the "loop body" of map must be a function call. Besides the syntactic benefit of list comprehensions, they are often as fast or faster than equivalent use of map.

Revelationist answered 15/6, 2013 at 8:36 Comment(7)
So mapping is the fastest way to do it?Mahaffey
Yes, for builtn-in functions it is generally faster, but LC are considered more pythonic and will work in both py2x and py3x. map returns an iterator in py3x.Revelationist
@AswinMurugesh Not always, but remember that list comprehensions & co are a map + a filter with two lambdas. If you plan only using a map with an internal function then map will be slightly faster than list comprehension. But when you DO mix all these, list comprehension do it better than you can do it yourself (if all that makes sense to you).Asexual
I can't get you. What do you mean by mix all these?Mahaffey
@AswinMurugesh Using map with a lambda + a filter with a lambda function. Because doing [e+1 for e in foo if e%3] is the same than map(lambda e: e+1, filter(lambda e: e%3, foo)) but LC are faster in that case (and more Pythonic, isn't it?)Asexual
@AswinMurugesh Don't forget to add @ followed by thePersonYouAreTalkingTo when it isn't the author of the answer but another commentator, otherwise I'm not noticed and I could have never seen your questions/answers.Asexual
@AswinMurugesh If you're using this is in a programming competition then I'll also suggest you to use sys.std.readline instead of raw_input.Revelationist
P
9

List comprehensions!

Intuitive and pythonic:

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

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

Progression answered 24/5, 2014 at 19:58 Comment(0)
R
6

If you're using map with built-in function then it can be slightly faster than 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 user-defined 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 comparisons:

>>> 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 page:

The only restriction is that the "loop body" of map must be a function call. Besides the syntactic benefit of list comprehensions, they are often as fast or faster than equivalent use of map.

Revelationist answered 15/6, 2013 at 8:36 Comment(7)
So mapping is the fastest way to do it?Mahaffey
Yes, for builtn-in functions it is generally faster, but LC are considered more pythonic and will work in both py2x and py3x. map returns an iterator in py3x.Revelationist
@AswinMurugesh Not always, but remember that list comprehensions & co are a map + a filter with two lambdas. If you plan only using a map with an internal function then map will be slightly faster than list comprehension. But when you DO mix all these, list comprehension do it better than you can do it yourself (if all that makes sense to you).Asexual
I can't get you. What do you mean by mix all these?Mahaffey
@AswinMurugesh Using map with a lambda + a filter with a lambda function. Because doing [e+1 for e in foo if e%3] is the same than map(lambda e: e+1, filter(lambda e: e%3, foo)) but LC are faster in that case (and more Pythonic, isn't it?)Asexual
@AswinMurugesh Don't forget to add @ followed by thePersonYouAreTalkingTo when it isn't the author of the answer but another commentator, otherwise I'm not noticed and I could have never seen your questions/answers.Asexual
@AswinMurugesh If you're using this is in a programming competition then I'll also suggest you to use sys.std.readline instead of raw_input.Revelationist
M
2

You can use this:

s = raw_input().split()
s = [int(i) for i in s]
Morphophoneme answered 3/4, 2017 at 18:42 Comment(0)
B
-1
def pairs(a,k):
  answer = 0
  s = set(a)
  for v in s:
    if v+k in s:
        answer += 1

  return answer

n, k = map(int, raw_input().split())
b = map(int, raw_input().split())
print pairs(b, k)
Backup answered 6/9, 2018 at 12:53 Comment(1)
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.Decorticate

© 2022 - 2024 — McMap. All rights reserved.