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?

4
  • You don't get the conversions in C/C++ for free either. Commented Jun 15, 2013 at 8:37
  • No but C/C++ can recognize multiple integers when they are seperated by space. So, I can get them as multiple integers directly. Commented Jun 15, 2013 at 8:38
  • It doesn't "recognize" them, it has to parse the string. Commented Jun 15, 2013 at 8:38
  • 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 C Commented Jun 15, 2013 at 8:39

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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.

7 Comments

So mapping is the fastest way to do it?
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.
@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).
I can't get you. What do you mean by mix all these?
@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?)
|
2

You can use this:

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

Comments

-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)

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.