I tried to change from Python 2.7 to 3.3.1
I want to input
1 2 3 4
and output in
[1,2,3,4]
In 2.7 I can use
score = map(int,raw_input().split())
What should I use in Python 3.x?
I tried to change from Python 2.7 to 3.3.1
I want to input
1 2 3 4
and output in
[1,2,3,4]
In 2.7 I can use
score = map(int,raw_input().split())
What should I use in Python 3.x?
input()
在 Python 3中使用。raw_input
已重命名为input
在 Python 3 中。现在返回一个迭代器而map
不是列表。
score = [int(x) for x in input().split()]
或者 :
score = list(map(int, input().split()))
作为一般规则,您可以使用 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()))
输出不一定是惯用的(列表理解在这里更有意义),但它会提供一个不错的起点。