在Ruby中我可以使用
x = gets.split(" ").map{|x| x.to_i}
Run Code Online (Sandbox Code Playgroud)
如何用Python编写
在Python 3.x中
list(map(int, input().split()))
Run Code Online (Sandbox Code Playgroud)
在Python 2.x中
map(int, raw_input().split())
Run Code Online (Sandbox Code Playgroud)
x = [int(part) for part in input().split()]
Run Code Online (Sandbox Code Playgroud)
在2.x中,使用raw_input()而不是input()- 这是因为在Python 2.x中,input()将用户的输入解析为Python代码,这是危险且缓慢的.raw_input()只是给你字符串.在3.x中,他们改变input()了工作方式,因为这是你通常想要的.
这是一个简单的列表推导,它采用输入的拆分组件(使用str.split(),在空白上拆分)并使每个组件成为整数.