0
What wrong with this code? Its not sorting the array properly
import numpy as np data = np.array([1000, 2500, 1400, 1800, 900, 4200, 2200, 1900, 3500]) new_house = input() data = np.append(data,new_house) data=np.sort(data) print(data)
2 Respostas
+ 5
Hi! I don’t know what you mean with ”properly”, but they are sorted as class numpy.str_ objects, so ’4200’ < ’900’. If they where numbers, 900 < 4200.
The input is always a string, and when you add it to your np.array ’data’, it seems to convert all its objects in ’data’ from numpy.int64 to numpy.str_ objects; an idea with ndarrays is to have a collection of items of the same type. So to convert the inputs to integers before you append it to your ’data’ array seems to be a good idea, if you want to keep all items in the np.array as numpy.int64 objects.
+ 4
Per Bratthammar
Yes , u toled right 👍. I changed the input from str type to int type then it worked. Actually numpy stores same data type, I forgot that 😁. Thank you.