strub
(strub)
February 10, 2012, 11:03am
#1
Hi,
I want to find out which is the lowest value and thought I could sort a list and then get the first value of the list. But somehow the sort() does not work.
str1 = '30.4'
str2 = '10.22'
str3 = '2.87'
list = [str1, str2, str3]
list.sort(key=str.lower)
lowest_value = list[0]
Does anybody know why? Or even has a better solution to find out the lowest string number? Thanks in advance
agoose77
(agoose77)
February 10, 2012, 11:11am
#2
.lower returns the lowercase version of a string.
You want to use the min function.
e.g
smallest_value = min([1, 4, 8]) # Returns 1
strub
(strub)
February 10, 2012, 11:19am
#3
hey thanks a lot for that but still I get 10.22 as the lowest instead of 2.87. I assume because it counts only the first number.
But how can I prevent that?
strub
(strub)
February 10, 2012, 11:25am
#4
aha I changed the strings to floats and now it works. Thanks again for that