Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python_uu2;在单行中提示用户多个变量,并在新行中调用每个变量_Python - Fatal编程技术网

Python_uu2;在单行中提示用户多个变量,并在新行中调用每个变量

Python_uu2;在单行中提示用户多个变量,并在新行中调用每个变量,python,Python,各位。 问这个问题我很紧张,我不确定这个标题是否能很好地解释我的问题内容。。 无论如何,这是我的问题 e、 g。 我想收到5个变量,但在单独的行,但我想在一行声明所有的变量 a,b,c,d,e = int(input(); // I know this won't work.. but this is the way kind of what i want. 这是我的输入 10 // this is assigned to a 40 // assigned to b 30 // assigne

各位。 问这个问题我很紧张,我不确定这个标题是否能很好地解释我的问题内容。。 无论如何,这是我的问题

e、 g。 我想收到5个变量,但在单独的行,但我想在一行声明所有的变量

a,b,c,d,e = int(input(); // I know this won't work.. but this is the way kind of what i want.
这是我的输入

10 // this is assigned to a
40 // assigned to b
30 // assigned to c
50 // assigned to d
20 // assigned to e
通常情况下,我以单线输入,以“”分隔,如下所示

10 40 30 50 20
a,b,c,d,e = map(int,input().split());
>>> x, y, z = input(), input(), input()
40
30
10
>>> x
'40'
>>> y
'30'
>>> z
'10'
我通常收到如下输入

10 40 30 50 20
a,b,c,d,e = map(int,input().split());
>>> x, y, z = input(), input(), input()
40
30
10
>>> x
'40'
>>> y
'30'
>>> z
'10'
但这次我想按照我的要求尝试一些不同的方法

//////////////////// 我找到了一些与这个问题相关的文章。 有如下所述

10 40 30 50 20
a,b,c,d,e = map(int,input().split());
>>> x, y, z = input(), input(), input()
40
30
10
>>> x
'40'
>>> y
'30'
>>> z
'10'
但这并不令人满意,因为它使用了太多的input()。 我想也许有某种方法可以将input()减少为1或其他任何形式

是否有解决方案指南? 谢谢你一直读到现在!
祝你今天愉快

您可以使用内置的
map()
函数,该函数执行作为iterable参数传递的函数

例如,在这里,您可以执行以下操作:

>>> a, b, c = tuple(map(input, range(3)))
01
12
23
>>> a
'1'
>>> b
'2'
>>> c
'3'
range(3)
创建一个由3个整数组成的生成器,该生成器与输入函数一起传递到
map()
。缺少
()
表示我们现在不想执行该函数,我们只想通知
map()
这是它应该为每次迭代执行的。该批次包含在
tuple()
中,以触发map函数返回的map对象的执行


请注意,上面显示的输入之前的“0”、“1”、“2”是由Python自动生成的,它们不是手动输入的。

这也可以使用列表理解来完成:

a,c,b,d,e = [input() for i in range(0,5)]

a、 b、c、d、e变量将包含来自用户的输入

您可以使用以下代码段行:

a, b, c, d, e = [input() for i in range(5)]

右侧创建了一个由5个元素组成的数组,每个元素调用函数
input()
,该函数的值属于该函数。

您可能应该改为使用该函数

  >>>a,b,c,d,e,f = [int(x) for x in input().split()]
这将输出


非常感谢。我想这就是我要找的!与其他解决方案相比,这似乎只使用了一次input()!哈哈,谢谢!