Reputation: 780
I am having an issue with my input method, I am trying to get a list of ints seperated by spaces. My code is below:
def numberStore():
numberList = input("Enter list of numbers seperated by spaces ").split(" ")
print("The lowest number is" , listLowestNumber(numberList))
I then have a function to return the lowest number of the list that has been input.
def listLowestNumber(list):
list.sort()
return list[0]
However when I execute the function numberStore, it only seems to sort the numbers by the first digit, for example entering the values 40 9 50 will return a value of 40 as the lowest number. Thanks for any help!
Upvotes: 2
Views: 9348
Reputation: 26184
You need to convert values on your list to int before you sort them:
numberList = input("Enter list of numbers seperated by spaces ").split()
numberList = [int(v) for v in numberList]
Upvotes: 1
Reputation: 1
Looks like it's sorting by treating the elements as strings. What you describe is lexicographic ordering, not numerical ordering. Basically you have to convert your string elements into numerical elements.
Upvotes: 0
Reputation: 500437
To sort a list of integers represented as strings, one could use:
l.sort(key=int)
Without the key=int
, the list gets ordered lexicographically.
However, if all you need to do is find the smallest number, the better way is
return min(l, key=int)
P.S. I've renamed your list to l
since list()
is a built-in name, and it's poor style to shadow built-in names.
Upvotes: 3