简体   繁体   English

如何仅从一行获得原始输入?

[英]How can I take raw input from only one line?

I want to type input from my keyboard in the PowerShell like this: 1 2 3 4 but if I type it like that, it will show this: 我想像这样在PowerShell中从键盘输入输入:1 2 3 4但如果这样输入,它将显示如下:

Traceback (most recent call last):
  File "iqtest.py", line 7, in <module>
    l.append(int(raw_input()))
ValueError: invalid literal for int() with base 10: '1 2 3 4'

It only works when I enter after each input, thus creating 4 lines instead of 1. How can I take all 4 input just in one line? 仅当我在每次输入后输入时才起作用,因此创建了4行而不是1行。如何将全部4个输入仅放在一行中? My code: 我的代码:

list=[]
l=list
for i in range(0,n):
    l.append(int(raw_input()))

您可以使用splitmap

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

Here I am reading multiple numbers separated by space So I am using .split(' ') which will return a list of numbers but of type string. 在这里,我正在读取多个以空格分隔的数字,所以我在使用.split(''),它将返回数字列表,但类型为string。 The next step is to use list comprehension to convert all the numbers in the list "x" into values of type int. 下一步是使用列表推导将列表“ x”中的所有数字转换为int类型的值。

x=raw_input().split(' ') 
x=[int(num) for num in x]
print x #python2

您可以使用splitlist comprehension `

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何有效地使用 input().split() 在一行中从用户那里获取至少 2 个输入? - How can i use input().split() effectively to take at least 2 inputs from user in one line? 如何在打印行上方获取用户的输入? - How can I take input from the user above a printed line? 在python中,如何从用户那里获取输入并在输入后跳转到不同的行? - In python how can I take input from user and jump to a different line after taking input? 如何从 Python 在一条线上获取多个或单个输入 - How to take either multiple or single input on one line from Python 如何在python中使用raw_input将元组作为输入? - How can I take tuple as input using raw_input in python? 如何从 Codeforces 获取多行输入? - How do I take multi-line input from Codeforces? 如何从 txt 文件中一次一行地获取数据并将其用作发送请求的变量? - How can i take data one line at a time from a txt file and use it as a variable to send requests? 我们如何在一行中使用原始空格输入原始输入,然后将它们转换为整数并将它们添加到列表中? - How can we take raw input in a single line with a whitespace between them, then convert them into integers and add them to a list? 如何从raw_input创建列表? - How can I create a list from raw_input ? 您如何从 Alexa 设备获取原始语音输入 - How do you take raw voice input from an Alexa device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM