2013-05-13 49 views
3

我試圖從Python 2.7版更改爲3.3.1輸入int列表3

我想輸入

1 2 3 4 

和輸出

[1,2,3,4] 

在2.7我可以使用

score = map(int,raw_input().split()) 

我應該在Python 3.x中使用什麼?

回答

17

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

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

或:

score = list(map(int, input().split())) 
1

一般情況下,你可以使用2to3 tool附帶Python來至少一點,你在正確的方向儘可能移植雲:

$ 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())) 

輸出不一定是慣用的(列表理解在這裏會更有意義),但它會提供一個體面的起點。