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中使用什麼?
我試圖從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中使用什麼?
在Python 3中使用input()
。raw_input
已在Python 3中重命名爲input
。現在map
返回一個迭代器而不是列表。
score = [int(x) for x in input().split()]
或:
score = list(map(int, input().split()))
一般情況下,你可以使用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()))
輸出不一定是慣用的(列表理解在這裏會更有意義),但它會提供一個體面的起點。