fast way to get input from stdin - Python

I am looking for faster way for taking input from stdin in Python. So far I found three methods. input() - which is suitable for integers, raw_input() - reads the input as string and another one is sys.stdin.readline(). So far I have found sys.stdin.readline() is the fastest. Do you have any idea to read input (from console) in Python which is faster?

Comments

Tamim Shahriar said…
Another way to take console input:
import sys

for line in sys.stdin:
# do whatever you want with line

Note that there is an extra newline character ("\n") after end of each line.
saikat said…
The first line contains the number of test cases T. T lines follow, one corresponding to each test case, containing 3 integers : N, A and D.

SAMPLE INPUT

3
1 1 1
3 5 6
2 1 2


how to take this input??
Tamim Shahriar said…
t = int(raw_input())

while t:
t = t - 1
s = raw_input()
n, a, d = s.split()
n = int(n)
a = int(a)
d = int (d)
Tamim Shahriar said…
All the lines after while loop are inside while loop. Can't find any way to format it.
saikat said…
combining these lines in one:


n, a, d = [int(x) for x in raw_input().split()]
saikat said…
leading dots can be used to keep correct format.



while t:
....t = t - 1
....s = raw_input()
....n, a, d = s.split()
Unknown said…
for T in range(input()):
N, A, D = map(int, raw_input().split())

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code