Python - Returning max number in an array. Errors
Question: 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 
    


Answer: **Edit:** Sorry, misinterpreted the question at first - see [this
answer](http://stackoverflow.com/questions/1714027/version-number-comparison)
for a few methods regarding comparing version numbers. Taking the second
answer in there (which suggests using `distutils.version`'s `StrictVersion`),
you can try this:

    
    
    In [3]: from distutils.version import StrictVersion
    
    In [4]: s = ['1.04','1.6','1.14', '2.02.04', '1.14.01']
    
    In [5]: max(s, key=StrictVersion)
    Out[5]: '2.02.04'
    

* * *

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 `max` function:

    
    
    In [1]: s = ['1.04','1.6','1.14']
    
    In [2]: max(s, key=float)
    Out[2]: '1.6'
    

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:

    
    
    In [4]: s = ['1.04','1.6','1.14']
    
    In [5]: s_floats = [float(x) for x in s]
    
    In [6]: s_floats
    Out[7]: [1.04, 1.6000000000000001, 1.1399999999999999]
    

You can then use max as you expect:

    
    
    In [9]: max(s_floats)
    Out[9]: 1.6000000000000001
    

