I am a Python 2.7 user who recently switched to python3. While reading integers separated by a blackspace/endline I used nex = iter(map(int,stdin.read().split())).next
, where nex()
acts as a function to input integers (Suppose for inputting an integral value in x -> x=nex()
. But in python3 this doesn't seem to work. Someone please propose a workaround for using the same in Python3.
Asked
Active
Viewed 35 times
0

intboolstring
- 6,891
- 5
- 30
- 44

markroxor
- 5,928
- 2
- 34
- 43
-
Note that `map` is already an iterator in Python 3. Consider reading the docs on the changes, particularly https://docs.python.org/3/whatsnew/3.0.html#operators-and-special-methods – jonrsharpe Sep 26 '15 at 14:36
-
you don't need to read all input, to get an iterator, see [How to read tokens without reading whole line or file](http://stackoverflow.com/q/20019503/4279) – jfs Sep 26 '15 at 14:41
1 Answers
1
.next()
method is called .__next__()
in Python 3. You could use next()
function to write single-source Python 2/3 compatible code:
from functools import partial
nex = partial(next, iter(iterable))
print(nex())

jfs
- 399,953
- 195
- 994
- 1,670