9
Suppose the following:
import numpy as np
a = np.array ( [1.1, 2.2, 3.3] )
How to convert this array to int without having to iterate each element or using another array?
Why do:
b = int(a)
Gives an error because it is only possible to convert an array of length 1 to int. So I try:
for i in a:
int(i)
# ou
# i = int(i)
does not solve, because a, after the loop remains with the elements in float.
You would then have to use one more array to do this and iterate over this array:
b = np.zeros( a.shape )
for i in xrange(0, len(a))
b[i] = int(a[i])
Returns me an array of integer numbers, yes, but still of type float, note the point...
print b
[1., 2., 3.]
How to then convert to int?
I don’t understand your answer... can you elaborate better? It seems that what you suggest to try, unlike my other answer, also gives error and ends up doing the same...
– LuizAngioletti
Yes, but there is one more option, in case. astype() do not solve, what can occur, I do not know why, more with me has happened and after doing so I had no more problems with it...
– Kenny Rafael
can you explain your answer better? I’m having trouble understanding how the error you present is relevant...
– LuizAngioletti
If there was any mistake it is because the first way did not work, then yes in this case I use the . astype()...
– Kenny Rafael
Hum! Now yes! Understood.
– LuizAngioletti
corrected the
Int, that there is no python me (2.7 at least), and I switched to'int'. But I couldn’t reproduce the mistake you showed.– LuizAngioletti
It does not have to necessarily occur, it is a prevention if it occurs, because it may occur, I did not find out why, but when it occurred I managed to solve the way I put....
– Kenny Rafael