-1
output = []
stuff = ['candy', '1.3', '1.23']
floats = map(float, stuff[1:])
tuples = (stuff[0], floats)
output.append(tuples)
print(output)

instead of printing out [('candy',[1.3,1.23])] as intended, it prints out:

[('candy', <map object at 0x00000000038AD940>)]

I don't know whats wrong please show me the fix.

taesu
  • 4,482
  • 4
  • 23
  • 41
jb3navides
  • 137
  • 2
  • 3
  • 8

3 Answers3

1

Your problem is that you aren't converting the map to a list, try the following:

output = []
stuff = ['candy', '1.3', '1.23']
floats = map(float, stuff[1:])
tuples = (stuff[0], list(floats))
output.append(tuples)
print(output)

>>> output = []
>>> stuff = ['candy', '1.3', '1.23']
>>> floats = map(float, stuff[1:])
>>> tuples = (stuff[0], list(floats))
>>> output.append(tuples)
>>> print(output)
[('candy', [1.3, 1.23])]
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
1

In Python3 map returns a map object.

This is the way you can achieve what you want in Python3:

floats = list(map(float, stuff[1:]))

The output:

[('candy', [1.3, 1.23])]
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
0

This is Python 2 eval of map:

Python 2.7.10 (default, Jun 10 2015, 19:42:47) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> map(float, ['1.1','1.2'])
[1.1, 1.2]

This is a Python3 lazy eval of map:

Python 3.4.3 (default, Jun 10 2015, 19:56:14) 
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> map(float, ['1.1','1.2'])
<map object at 0x103da3588>

What you are seeing is because you are running your code on Python 3. Wrap with list to fix.