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?
Python list can contain duplicate elements.
Use input()
in Python 3. raw_input
has been renamed to input
in Python 3 . And map
now returns an iterator instead of list.
score = [int(x) for x in input().split()]
or :
score = list(map(int, input().split()))
As a general rule, you can use the 2to3
tool that ships with Python to at least point you in the right direction as far as porting goes:
$ 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With