On 04/25/2016 07:13 AM, oyster wrote:
for a simple code
[code]
vexList = [1, 2, 3]
print('vexList', list(vexList))
vexList=map(lambda e: e+1, vexList)
print('vexList', list(vexList))
vexList = list(vexList)
print('vexList', list(vexList))
vexList=map(lambda e: e*2,vexList)
print('vexList', list(vexList))
[/code]
py27 says
[quote]
('vexList', [1, 2, 3])
('vexList', [2, 3, 4])
('vexList', [2, 3, 4])
('vexList', [4, 6, 8])
[/quote]
but py34 says
[quote]
vexList [1, 2, 3]
vexList [2, 3, 4]
vexList []
vexList []
[/quote]
The difference in behaviour between Python2 and Python3 is the map
function. In P2 it returned a list, while in P3 it returns an
iterator. Your code runs through that iterator twice with the list()
function. The first time it gets the elements of the list as expected,
but the second time, the iterator is exhausted and returns no objects.
A simpler example: "b" is a map object (iterator), then list(b) is run
twice.
>>> a = [1,2,3]
>>> b = map(lambda e: e+1, a)
>>> b
<map object at 0x7f3840a0fc88>
>>> list(b)
[2, 3, 4]
>>> list(b)
[]
I hope that helps.
Gary Herron
--
Dr. Gary Herron
Professor of Computer Science
DigiPen Institute of Technology
(425) 895-4418
--
https://mail.python.org/mailman/listinfo/python-list