在 Python 3 中输入 int 列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16525327/
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
Input int list in Python 3
提问by lifez
I tried to change from Python 2.7 to 3.3.1
我试图从 Python 2.7 更改为 3.3.1
I want to input
我要输入
1 2 3 4
and output in
并输出
[1,2,3,4]
In 2.7 I can use
在 2.7 我可以使用
score = map(int,raw_input().split())
What should I use in Python 3.x?
我应该在 Python 3.x 中使用什么?
回答by Ashwini Chaudhary
Use input()in Python 3. raw_inputhas been renamed to inputin Python 3 . And mapnow returns an iterator instead of list.
input()在 Python 3 中使用。raw_input已重命名为input在 Python 3 中。而map现在返回列表的迭代器代替。
score = [int(x) for x in input().split()]
or :
或者 :
score = list(map(int, input().split()))
回答by Cairnarvon
As a general rule, you can use the 2to3toolthat ships with Python to at least point you in the right direction as far as porting goes:
作为一般规则,就移植而言,您可以使用Python 附带的2to3工具至少为您指明正确的方向:
$ echo "score = map(int, raw_input().split())" | 2to3 - 2>/dev/null
--- <stdin> (original)
+++ <stdin> (refactored)
@@ -1,1 +1,1 @@
-score = map(int, raw_input().split())
+score = list(map(int, input().split()))
The output isn't necessarily idiomatic (a list comprehension would make more sense here), but it will provide a decent starting point.
输出不一定是惯用的(列表理解在这里更有意义),但它会提供一个不错的起点。

