python 3: generator for map
Question:
I’ve got trouble with using generator on map object. This is the simpliest example:
a = ['1','2','3']
a = map(int, a)
for x in a:
print(x, end = ' ') #output 1 2 3
b = [x for x in a]
print(b) #output []
Python 3.5
P.S. Of course I know about list(map) but I want to know why this doesn’t work.
Answers:
Because the map object got exhausted after your printed everything in it. You can only iterate through it once.
I’ve got trouble with using generator on map object. This is the simpliest example:
a = ['1','2','3']
a = map(int, a)
for x in a:
print(x, end = ' ') #output 1 2 3
b = [x for x in a]
print(b) #output []
Python 3.5
P.S. Of course I know about list(map) but I want to know why this doesn’t work.
Because the map object got exhausted after your printed everything in it. You can only iterate through it once.