I have a script to pull random numbers from a set of values. However, it broke today because min()
and max()
sort values by lexicographic order (so 200 is considered greater than 10000). How can I avoid lexicographic order here? Len
key is on the right track but not quite right. I couldn't find any other key(s) that would help.
data_set = 1600.csv, 2405.csv, 6800.csv, 10000.csv, 21005.csv
First try:
highest_value = os.path.splitext(max(data_set))[0]
lowest_value = os.path.splitext(min(data_set))[0]
returns: lowest_value = 10000
highest_value = 6800
Second try:
highest_value = os.path.splitext(max(data_set,key=len))[0]
lowest_value = os.path.splitext(min(data_set,key=len))[0]
returns: lowest_value = 1600
highest_value = 10000
Thanks.
You can use
key
to order by the numeric part of the file:You were close. Rather than using the result of
splittext
with thelen
function, use theint
function instead:Of course, this solution assumes that your file name will consist solely of numerical values.