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中读取数字?_Python - Fatal编程技术网

如何方便地在python中读取数字?

如何方便地在python中读取数字?,python,Python,我的问题是读6个数字,每个数字都在一行上。如何比我上面的代码更简洁地做到这一点 x1, y1, a1, b1, x2, y2 = int(input()), int(input()), int(input()), int(input()), int(input()), int(input()) 在Python 2中,将range替换为xrange,将input替换为raw\u input。我会使用字典: x1, y1, a1, b1, x2, y2 = (int(input()) for _

我的问题是读6个数字,每个数字都在一行上。如何比我上面的代码更简洁地做到这一点

x1, y1, a1, b1, x2, y2 = int(input()), int(input()), int(input()), int(input()), int(input()), int(input())

在Python 2中,将
range
替换为
xrange
,将
input
替换为
raw\u input

我会使用字典:

x1, y1, a1, b1, x2, y2 = (int(input()) for _ in range(6))
x,y,z,w=map(int,input().split()) #add input in form 1 2 3 4


>>> x,y,z,w=map(int,input().split()) 
1 2 3 4
>>> x
1
>>> y
2
>>> w
4
>>> z
3
然后可以将字典键转换为变量,但我认为这不是一个好主意:

parm = {}
var_names = ['x1', 'y1', 'a1', 'b1', 'x2', 'y2']
for var_name in var_names:
    parm[var_name] = int(input())

在python2中,您不需要
int
,除非您将
input()
替换为
raw\u input()
——您肯定应该这样做……数字不是一行给出的:)您不应该修改
vars()
返回的dict,因为它与
locals()相同
,文档中对此有明确警告。如果您想以正确的方式编写错误的代码,可以使用
globals().update(parm)
。。。
locals().update(parm)