4

I was trying to write a million digits of pi in an array with each digit as an individual element of the array. The values are loaded from a '.txt' file from my computer. I have seen a similar question here. Written by the help of this, my code is:

import numpy as np
data = np.loadtxt('pi.txt')
print(map(int, str(data)))

But the output is this:

<map object at 0x0000000005748EB8>

Process finished with exit code 0

What does it mean?

Sahil
  • 256
  • 1
  • 7

2 Answers2

2

A few operations with Python version 3 become "lazy" and for example mapping now doesn't return a list of values but a generator that will compute the values when you iterate over it.

A simple solution is changing the code to

print(list(map(int, str(data))))

This is quite a big semantic change and of course the automatic 2->3 migration tool takes care of it... if you programmed in Python 2 for a while however is something that will keep biting you for quite some time.

I'm not sure about why this change was considered a good idea.

6502
  • 112,025
  • 15
  • 165
  • 265
1

map in Python 3 will give you a generator, not a list, but in python 2 it will give a list. The stack overflow link you have given refers to Python 2 where as you are writing code in python 3.

you can refer to these ideone links.

python 2 https://ideone.com/aAhvLD

python 3 https://ideone.com/MjA5nj

so if you want to print list you can do

print(list(map(int, str(data))))