Python 2 中的整数数组输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27675035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integer Array Input in Python 2
提问by user220789
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.
我是python的新手。我想获取 2 个大小为 4 的整数数组 a 和 b 的用户输入并打印它们。输入应以空格分隔。
First user should input array a[] like this:
第一个用户应该像这样输入数组 a[]:
1 2 3 4
1 2 3 4
The he should input array b[] like this
他应该像这样输入数组 b[]
2 3 4 6
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?
程序应该显示 a 和 b 作为输出。我希望 a 和 b 中的变量是整数而不是字符串。我该怎么做?
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.
但这不起作用。
回答by 6502
raw_inputreads a single line and returns it as a string.
raw_input读取一行并将其作为字符串返回。
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 交互式 shell 是实验其工作原理的好方法...
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]
>>> _
回答by Uri Goren
raw_input()reads a line from the user, that line needs to be splitted by space
raw_input()从用户读取一行,该行需要用空格分割
a = raw_input().split()
b = raw_input().split()
Next, You'll need to convert the data to intThe easiest way to do that, is list comprehension
接下来,您需要将数据转换为int最简单的方法是列表理解
a = [int(x) for x in a]
b = [int(x) for x in b]
回答by Tanveer Alam
Your program is working fine. You just didn't pass the prompt stringwhich gets prompted on terminal to ask user's input:
你的程序运行良好。您只是没有通过prompt string在终端上提示询问用户输入的内容:
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 integersas input you can use inputbuilt-in function, by which there is no need to type cast it again to integer.
如果您只希望integers作为输入,您可以使用input内置函数,这样就无需再次将其强制转换为整数。
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)).
回答by Erin
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
回答by Rajeev Kumar Singh
You can use python's sys module for reading input from command line.
您可以使用 python 的 sys 模块从命令行读取输入。
import sys;
a = map(int, sys.stdin.readline().split());
b = map(int, sys.stdin.readline().split());
print a;
print b;
回答by jayant singh
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
6
2 3 9 10 12 15
OUTPUT
输出
2
3
9
10
12
15
2
3
9
10
12
15

![Python:A[1:] 中的 x 是什么意思?](/res/img/loading.gif)