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 Answers
+ 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.