Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing million digits of pi in an array. Output : "<map object at 0x0000000005748EB8>" [duplicate]

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?

like image 866
Sahil Avatar asked Oct 18 '25 16:10

Sahil


2 Answers

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.

like image 159
6502 Avatar answered Oct 20 '25 06:10

6502


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))))
like image 24
Nagasitaram Thigulla Avatar answered Oct 20 '25 06:10

Nagasitaram Thigulla