7

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?

4

2 回答 2

28

input()在 Python 3中使用。raw_input已重命名为input在 Python 3 中。现在返回一个迭代器而map不是列表。

score = [int(x) for x in input().split()]

或者 :

score = list(map(int, input().split()))
于 2013-05-13T15:07:49.957 回答
1

作为一般规则,您可以使用 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()))

输出不一定是惯用的(列表理解在这里更有意义),但它会提供一个不错的起点。

于 2013-05-13T15:20:15.043 回答