如何方便地在python中读取数字?
我的问题是读6个数字,每个数字都在一行上。如何比我上面的代码更简洁地做到这一点如何方便地在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 _
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)