Friday, 15 April 2011

using map in python 3 -


Hi This code works on Python 2.7, but not in Python 3

  import Itertools def product (a, b): return map (list, iterolol. Product (a, repeat = b) print (sorted (product ({0,1}, 3)))   

It outputs

  [[0, 0], [0, 0, 1], [0, 1, 0], [0, 1, 1] , [1, 0, 0] in Python 2.7, [1, 0, 1], [1, 1, 0], [1, 1, 1]]   

but Python 3 It gives a map object at 0x028DB2F0, anyone know how it works for Python 3 So that the output should remain similar to Python 2.7.

Just wrap it with a list: itertools def product (a, b): return list (map, list, itertools.product (a, repeat = b)) print (sorted (product ({0,1}, 3)))

Find more

In two words in Python 3 map

Return an Iterator, which works in every object Implemented, provides results.

In Python 2.7,

Apply the job of every object and return a list of results.

No comments:

Post a Comment