+ 3
Square Footage #python for data science
You are given an array that holds the square footage data for houses on a particular street. A new house has just been constructed on that street. Modify your program to take the new house value as input, add it to the array, and output the array sorted in ascending order. May I please ask where my error is ############### import numpy as np data = np.array([1000, 2500, 1400, 1800, 900, 4200, 2200, 1900, 3500]) new = input() data = np.append(data,new) data = np.sort(data) print(data)
13 Respostas
+ 5
LI HAN ,
your code is ok, except the input, that should be int:
new = int(input()
+ 6
Freddy Fernando Maldonado Huicho ,
it would be better if you start a new feed with your question. but i can give you an answer related to appending a new value as int or string:
if the new value is input as an int, the result will be:
1900
[1000 2500 1400 1800 900 4200 2200 1900 3500 1900]
[ 900 1000 1400 1800 1900 1900 2200 2500 3500 4200]
if the new value is input without converting it to int, all values in the array are strings:
'1900''
['1000' '2500' '1400' '1800' '900' '4200' '2200' '1900' '3500' '1900']
['1000' '1400' '1800' '1900' '1900' '2200' '2500' '3500' '4200' '900'] <<<< sorting
in case values are strings, the sorting is done lexicographically, so 900 is the greatest value, because it starts with the greatest digit.
+ 5
Simon Sauter ,
are you sure that your suggestion is working? have you tested it? we are talking here about a numpy array.
+ 5
Lavanya 2000 ,
you posted 100% exactly the same code as ali abdollahi 1 month ago ?
+ 3
The answer is this
import numpy as np
data = np.array([1000, 2500, 1400, 1800, 900, 4200, 2200, 1900, 3500])
new = int(input())
data = np.append(data,new)
data = np.sort(data)
print(data)
+ 1
correct answer
import numpy as np
data = np.array([1000, 2500, 1400, 1800, 900, 4200, 2200, 1900, 3500])
new = int(input())
data = np.append(data,new)
data = np.sort(data)
print(data)
+ 1
May I ask why when I dont punt int before the input function the array it isn't sort properly?. I saw that the only number wasn't properly sorted was the "900". But why? If the number input was "1100". It is like the number "900" already given in the array was a text. I am confused.
+ 1
import numpy as np
data = np.array([1000, 2500, 1400, 1800, 900, 4200, 2200, 1900, 3500])
newhouse=int(input())
data = np.append(data, newhouse)
data = np.sort(data)
print(data)
0
No. I think my post was wrong.
0
Thank you !! It works. Lothar I have forgotten the basic input code haha
0
Thank you Simon Sauter for the quick reply. Appreciated it.
0
import numpy as np
data = np.array([1000, 2500, 1400, 1800, 900, 4200, 2200, 1900, 3500])
data = np.append(data,int(input()))
print(np.sort(data))
0
import numpy as np
data = np.array([1000, 2500, 1400, 1800, 900, 4200, 2200, 1900, 3500])
data = np.append(data,int(input()))
print(np.sort(data))