share
Stack OverflowExact cin equivalent function in python
[-1] [3] tkg
[2011-01-14 13:44:30]
[ python cin ]
[ http://stackoverflow.com/questions/4691639] [DELETED]

Suppose user enter this string at terminal

123 456 456 //then hit enter

How do I scan these three (could be more) numbers in different variables in python

Could be something like this:

for i in range(1,n)
    m[i]=#WHAT FUNCTION SHOULD I PUT HERE

In c++ we can easily use cin>>m[i] inside above loop to scan the variables.

If i use input() or raw_input() , they would scan whole line in single variable.

Is there any way without using list. - tkg
(2) You will have to give us a rationale for not wishing to use a list, and tell us what you would like to use instead. - Sven Marnach
@ Sven Marnach: I am just worried if no of input integers is large then retrieving them from list would take time. - tkg
[+3] [2011-01-14 13:46:26] Sven Marnach

To get a list of these numbers, try

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

raw_input() will return the verbatim string as you enter it. The split() method of strings returns a list of strings split at any whitespace character. Finally, the map() invocation turns this list of strings into a list of integers.

If you are using Python 3.x, use input() instead of raw_input().

If you don't want to use a list, and you know the number of input values beforehand, you can use sequence unpacking to assign the values to different varaibles.

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

will work only if exactly three integers separated by whitespace are entered, and will assigne these three integers to the names a, b and c.


@Sven Marnach isn't there any other way then using list - tkg
Sven Marnach problem is that number of integers entered could vary upto 10^10 - tkg
(2) @gkt.pro: You would like to read in up to 10^10 integers from stdin in text format? You know this will take at least 40GB of RAM? Furthermore, this a completely different question than the one you asked. Please try again :) - Sven Marnach
@Sven Marnach I am not saying exactly 10^10, but can vary up to large number. - tkg
(1) @gkt.pro: Anyway, edit your question (or maybe better ask a new one) to detail your requirements. Do you need all integers in memory at the same time? What kind of access to the integers do you need? What is it you actually want to accomplish? - Sven Marnach
@Sven Marnach: Take this problem:spoj.pl/problems/BOOKS1/.Thats a long problem, just note that in input section we have to scan single line with integers p1, p2, ... pm separated by spaces. - tkg
@gkt.pro: So the number of integers is <= 500. That's quite a bit smaller than 10^10, and you should be fine with using a list. - Sven Marnach
Thanks @Sven Marnach for your precious time and advice. - tkg
1
[+3] [2011-01-14 15:08:15] Adrien Plisson [ACCEPTED]

if you do not want to scan the whole input in a list, you can write your own input parser, which reads one character at a time from the input stream:

def nextint(input):
    def skip(input):
        char = input.read(1)
        while str.isspace(char):
            char = input.read(1)
        return char
    char = skip(input)
    while 1:
        result = []        
        while str.isdigit(char):
            result.append(char)
            char = input.read(1)
        yield int(str.join('', result))
        char = skip(input)

now use it like this:

import sys

reader = nextint(sys.stdin)
for value in reader:
    # process the value here
    pass

(the generator will fail on the first block of non-space characters which cannot be converted to int)


Unfortunately sys.stdin is generally buffered, so the above won't work if there aren't any carriage returns in the input. You can work around this, although it's easier in python 3. - DSM
@DSM: you are right, but so so will the use input(), and i am pretty confident that cin in the C++ runtime does the same. - Adrien Plisson
Thanks @Adrien Plisson thats what I was looking for. - tkg
2
[+1] [2011-01-14 13:47:14] dan04

In Python 2.x:

m = [int(n) for n in raw_input().split()]

In 3.x:

m = [int(n) for n in input().split()]

isn't there any other way then using list - tkg
(1) @gkt.pro: Any reason why you don't want to use a list? - user225312
@sukhbir:Problem is that the number of values entered by user could be large and lots of CPU cycles will be wasted to extract the values from list. Actually a file containing the inputs is redirected into the programme, its an on line programming challenge - tkg
3