Integer Array Input in Python 2

26,784

Solution 1

raw_input reads a single line and returns it as a string.

If you want to split the line on spaces a solution is

a = raw_input().split()
b = raw_input().split()

note that them will be arrays of strings, not of integers. If you want them to be integers you need to ask it with

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

or, more explicitly

a = []
for x in raw_input().split():
    a.append(int(x))
b = []
for x in raw_input().split():
    b.append(int(x))

The Python interactive shell is a great way to experiment on how this works...

Python 2.7.8 (default, Sep 24 2014, 18:26:21) 
[GCC 4.9.1 20140903 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> "19 22 3 91".split()                                                        
['19', '22', '3', '91']
>>> map(int, "19 22 3 71".split())                                              
[19, 22, 3, 71]
>>> _

Solution 2

raw_input() reads a line from the user, that line needs to be splitted by space

a = raw_input().split()
b = raw_input().split()

Next, You'll need to convert the data to int The easiest way to do that, is list comprehension

a = [int(x) for x in a]
b = [int(x) for x in b]

Solution 3

Your program is working fine. You just didn't pass the prompt string which gets prompted on terminal to ask user's input:

a=[]
b=[]
for i in range(0,4):
    m=int(raw_input(" Enter value for a list :"))
    a.append(m)
for i in range(0,4):
    n=int(raw_input(" Enter value for b list :"))
    b.append(n)

print "list a looks like :-", a
print "list b looks like :-", b

This is how it will result:

 Enter value for a list :1
 Enter value for a list :2
 Enter value for a list :3
 Enter value for a list :4
 Enter value for b list :5
 Enter value for b list :6
 Enter value for b list :7
 Enter value for b list :8
list a looks like :- [1, 2, 3, 4]
list b looks like :- [5, 6, 7, 8]

raw_input(...)
    raw_input([prompt]) -> string

    Read a string from standard input.  The trailing newline is stripped.
    If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.
    On Unix, GNU readline is used if enabled.  The prompt string, if given,
    is printed without a trailing newline before reading.

If you are expecting only integers as input you can use input built-in function, by which there is no need to type cast it again to integer.

a=[]
b=[]
for i in range(0,4):
    m = input(" Enter value for a list :")
    a.append(m)
for i in range(0,4):
    n = input(" Enter value for b list :")
    b.append(n)


input(...)
    input([prompt]) -> value

Equivalent to eval(raw_input(prompt)).
Share:
26,784
user220789
Author by

user220789

Updated on July 09, 2022

Comments

  • user220789
    user220789 almost 2 years

    I am new to python. I want to take user inputs of 2 integer arrays a and b of size 4 and print them. The input should be space seperated.

    First user should input array a[] like this:

    1 2 3 4

    The he should input array b[] like this

    2 3 4 6

    The program should display a and b as output.I want the variables in a and b to be integers and not string.How do I this?

    I was trying something like this

     a=[]
     b=[]
     for i in range(0,4):
             m=raw_input()
             a.append(m)
     for i in range(0,4):
             n=int(raw_input())
             b.append(n)
    
     print a
     print b
    

    But this does not work.

  • user220789
    user220789 over 9 years
    Thank you sir for your answer. Can you please tell me exactly where i should place the "for i in range(0,4):" for taking the values of 4 space seperated inputs from the user and what parts should be enclosed within the loop
  • 6502
    6502 over 9 years
    @user220789: str.split() already does the needed loop internally and returns the list containing the spaces-separated parts; so raw_input.split() is already a list (but it's a list of substrings, not a list of ints).
  • user220789
    user220789 over 9 years
    So basically what happens here is that after I hit enter after the inputs, the new loop " for x in raw_input().split(): b.append(int(x))" starts?So according to my understanding no checking for upper limit of array size occurs here I can enter any number of integers for a. Only when I hit enter the new list b begins.
  • 6502
    6502 over 9 years
    @user220789: nothing happens when you hit space. Python raw_input always waits for you typing enter and then returns a string containing what you typed (including spaces): always a single string like e.g. "1 2 3 4". Calling split() on that string returns ["1", "2", "3", "4"], i.e. a list of strings and then you need to convert them to integers. This can be done compactly with map(int, ...) or explicitly like in the two loops I've shown.
  • user220789
    user220789 over 9 years
    that was a typo, Sir I meant enter only. I have corrected my comment now.Thank you for your help, Sir.
  • Rajeev Kumar Singh
    Rajeev Kumar Singh almost 9 years
    If you use python 3, you would need to do -> a = list(map(int, sys.stdin.readline().split())); b = list(map(int, sys.stdin.readline().split()));