-1

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 
David Cain
  • 16,484
  • 14
  • 65
  • 75
Ritashugisha
  • 93
  • 2
  • 9

4 Answers4

3

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'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
Community
  • 1
  • 1
RocketDonkey
  • 36,383
  • 7
  • 80
  • 84
  • Those lambda's aren't needed. `max(s, key=StrictVersion)` is same as `max(s, key=lambda x: StrictVersion(x))` and `max(s, key=float)` is same as `max(s, key=lambda x: float(x))` – James Waldby - jwpat7 Dec 27 '12 at 18:25
  • @jwpat7 Ah, you're totally right. I have no idea why but I do that literally every time (unnecessary lambdas) - let's hope this is the last time :) Thanks for pointing it out, will adjust. – RocketDonkey Dec 27 '12 at 18:26
1

You need to compare the major version followed by minor version in succession. Max function accepts a key argument which you can use to modify the comparable, by converting the string to a tuple of major and minor version

>>> max(array, key = lambda e: map(int, e.split(".")))
'1.14'
Abhijit
  • 62,056
  • 18
  • 131
  • 204
0

You could use a list where each program version would be stored as another list, as this:

list = [[1,14],[1,6],[1,4]]

where each version componnent is part of the inner list.

you could then sort it like this:

sortedlist = sorted(sorted(sorted(list, key=lambda x: x[2]), key=lambda x: x[1]),key=lambda x: x[0])
carlosavoy
  • 99
  • 4
0

In version numbers you have to play different, split numbers and compare by mayor_version first and minor_version later.

I'm pretty sure someone solved this time ago...

after a google search...

Yes!! Python guys indeed. Check this:

http://pydoc.org/get.cgi/usr/local/lib/python2.5/distutils/version.py

MGP
  • 2,981
  • 35
  • 34