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?

TerryA
  • 58,805
  • 11
  • 114
  • 143
Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69

4 Answers4

8

List comprehensions!

Intuitive and pythonic:

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

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

Community
  • 1
  • 1
phil-ociraptor
  • 1,041
  • 8
  • 5
5

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.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • So mapping is the fastest way to do it? – Aswin Murugesh Jun 15 '13 at 08:42
  • 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. – Ashwini Chaudhary Jun 15 '13 at 08:45
  • @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). – jeromej Jun 15 '13 at 08:46
  • I can't get you. What do you mean by `mix all these?` – Aswin Murugesh Jun 15 '13 at 08:47
  • @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?) – jeromej Jun 15 '13 at 08:50
  • @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. – jeromej Jun 15 '13 at 08:52
  • @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`. – Ashwini Chaudhary Jun 15 '13 at 08:57
2

You can use this:

s = raw_input().split()
s = [int(i) for i in s]
Tanay Agrawal
  • 392
  • 1
  • 9
-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)
Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
  • 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. – Nic3500 Sep 07 '18 at 03:46