I have a Python script that connects to a website via FTP and lists the current version numbers of programs located on the website. I created an array to hold the version numbers till the script would pick the largest number out of the array and tell me what it was. For example my array would usually look like this:
array = ['1.04','1.6','1.14']
So I used…
max_in_array = max(array)
to return the largest value in the array.
Unfortunately, I received ‘1.6’ as the largest number when, in fact, ‘1.14’ is the latest version number.
The only other thing I could think to do is to cut off the ‘1.’ from the list of arrays so that I receive:
array = ['04','6','14']
And then paste it back on to get the full version number. But since this website contains many versions such as ‘2.02.04’ and ‘1.14.01’ I don’t know how to efficiently do that.
Any help would be greatly appreciated!
os.chdir("./gnu/"+_package)
pope = len(_package) + 1 ## Cuts off name of program
char = len(_package) - 12 ## Cuts off extension ".tar.gz"
for tok in glob.glob("*.tar.gz"):
token.append(tok) ## Appends name to array
bork = max(token) ## Gets max version number (sometimes useless)
print bork
char = len(bork) - 7
for _gnu in glob.glob("*.tar.gz"): ## Finds only matches with .tar.gz
_gnuapp.append(_gnu[pope:char]) ## Appends version number to array
spoke = max(_gnuapp) ## Gets max version number
_ver = _package + "-" + spoke + ".tar.gz" ## Compiles the package name, ver #, and extension
_down(_user, _ver, _package) ## Opens information in download module
Edit: Sorry, misinterpreted the question at first – see this answer for a few methods regarding comparing version numbers. Taking the second answer in there (which suggests using
distutils.version‘sStrictVersion), you can try this:One thing to note is that you are actually comparing strings and not numbers. One thing you can do to ensure that you are comparing the number themselves (disregarding that you want to do with the item afterwards) is to provide a key to the
maxfunction:Since you are looking to compare float values (and not the strings), this compares the float equivalents of the strings. However it may be best to convert those to floats before proceeding:
You can then use max as you expect: