简体   繁体   中英

Integer Array Input in Python 2

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.

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]
>>> _

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]

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

From your description, I would code something like...

def foo():
    a = raw_input()
    a = a.split()
    a = [int(x) for x in a]
    if len(a) != 4:
            raise Exception("error: input 4 integers")
    b = raw_input()
    b = b.split()
    b = [int(x) for x in b]
    if len(b) != 4:
            raise Exception("error: input 4 integers")
    print a
    print b

You can use python's sys module for reading input from command line.

import sys;
a = map(int, sys.stdin.readline().split());
b = map(int, sys.stdin.readline().split());
print a;
print b;

Here i have the complete code with example

n=int(raw_input())
a=map(int,raw_input().split())
m=0
for i in range(n):
    print a[i]

INPUT

6

2 3 9 10 12 15

OUTPUT

2

3

9

10

12

15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM