在Python 2整数阵列输入 [英] Integer Array Input in Python 2

查看:147
本文介绍了在Python 2整数阵列输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的蟒蛇。我想利用2整型数组用户输入和大小4 b和打印。
输入要空间分隔。

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.

首先使用者应输入数组a []是这样的:

First user should input array a[] like this:

1 2 3 4

在他需要输入数组b []像这样

The he should input array b[] like this

2 3 4 6

该程序应显示A和B output.I要在a和b的变量是整数,而不是做string.How这个我?

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?

我是想这样的事情

 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()读取用户一条线,这条线需要通过空间劈裂

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

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

接下来,你需要将数据转换为 INT
要做到这一点最简单的方法,就是名单COM prehension

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]

这篇关于在Python 2整数阵列输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆