Python 如何在一行中读取由空格分隔的两个输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4162815/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:34:05  来源:igfitidea点击:

How to read two inputs separated by space in a single line?

python

提问by Prince Ashitaka

I want to read two input values. First value should be an integer and the second value should be a float.

我想读取两个输入值。第一个值应该是一个整数,第二个值应该是一个浮点数。

I saw Read two variables in a single line with Python, but it applies only if both the values are of same type. Do I have any other way?

我看到Read two variables in a single line with Python,但仅当两个值的类型相同时才适用。我还有什么办法吗?

Example input, first is int and second is float. The inputs should be on a single line:

示例输入,第一个是 int,第二个是 float。输入应该在一行上:

20 150.50

http://www.codechef.com/problems/HS08TEST/

http://www.codechef.com/problems/HS08TEST/

I'm very new to Python.

我对 Python 很陌生。

采纳答案by Gabi Purcaru

Like this:

像这样:

In [20]: a,b = raw_input().split()
12 12.2

In [21]: a = int(a)
Out[21]: 12

In [22]: b = float(b)
Out[22]: 12.2

You can't do this in a one-liner (or at least not without some super duper extra hackz0r skills -- or semicolons), but python is not made for one-liners.

你不能在单行(或者至少在没有一些超级骗子的额外 hackz0r 技能——或分号)中做到这一点,但 python 不是为单行写的。

回答by dietbuddha

One liner :)

一个班轮:)

>>> [f(i) for f,i in zip((int, float), raw_input().split())]
1 1.2
[1, 1.2]

回答by Phoris

Simpler one liner(but less secure):

更简单的单衬(但不太安全):

map(eval, raw_input().split())

回答by Anonymous V

map(str,input().split())that is how you do it.

map(str,input().split())这就是你的方式。

回答by RITIK DWIVEDI

If the input is separated by spaces " "

如果输入以空格“”分隔

a,b,c = raw_input().split(" ")

a,b,c = raw_input().split(" ")

If the input is separated by comma ','

如果输入以逗号 ',' 分隔

a,b,c = raw_input().split(",")

回答by Jasmohan

In Python 2.7, I use this

在 Python 2.7 中,我使用这个

A,B = raw_input().split(" ")

A = int(A)

B = float(B)

print(A)

print(B)

Output

输出

34 6.9

34 6.9

34

34

6.9

6.9

回答by sems

If you wish to take as many inputs as u want then following:

如果您希望获得尽可能多的输入,请执行以下操作:

    x=list(map(str,input().split())) 
    print(x)

If you want two inputs:

如果你想要两个输入:

   x,y=x,y=list(map(str,input().split()))
   print(x,y)

回答by Maciej Bled

This is good solution imho a, b = input().split().

恕我直言,这是一个很好的解决方案a, b = input().split()

If you want to separate input with custom character you can put it in parenthesis e.g. a, b = input().split(",")

如果你想用自定义字符分隔输入,你可以把它放在括号中,例如 a, b = input().split(",")

回答by Mahdi Jewel

Read 3 inputs separated by space...

读取由空格分隔的 3 个输入...

arr = input().split(" ")
A = float(arr[0])
B = float(arr[1])
C = float(arr[2])
print(A)
print(B)
print(C)

回答by vijayraj34

Python 3.5

蟒蛇 3.5

Below snippet works for me.

下面的片段对我有用。

a, b = input().split(" ")
a_value = int(a)
b_value = int(b)