Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to do a numeric sort
#11
I did this in 15 minutes to wake up..

Output:
andre@andre-GP70-2PE:~$ python by_version.py {(2, 4): ['python2.4.1-foo.gz'], (3, 4): ['python3.4.0-foo.gz', 'python-3.4.2-foo.gz', 'python-3.4.3-foo.gz', 'python-3.4.4-foo.gz', 'python-3.4.5-foo.gz', 'python-3.4.6-foo.gz', 'python-3.4.7-foo.gz', 'python-3.4.8-foo.gz', 'python-3.4.9-foo.gz', 'python-3.4.10foo.gz'], (3, 5): ['python-3.5.0-foo.gz', 'python-3.5.1-foo.gz', 'python-3.5.2-foo.gz', 'python-3.5.3-foo.gz', 'python-3.5.4-foo.gz', 'python-3.5.5-foo.gz', 'python-3.5.6-foo.gz', 'python-3.5.7-foo.gz']}
import re
from itertools import groupby
from pprint import pprint


pattern = re.compile(r'python-?(\d+)\.(\d+)\.(\d+)-?.+\.gz')


def by_version(version_string):
    match = pattern.search(version_string)
    if match:
        return tuple(int(v) for v in match.groups())
    else:
        return 0, 0, 0


def sort_by_major_minor(versions):
    sorted_versions = sorted(versions, key=by_version)
    result = {}
    for major_group, major in groupby(sorted_versions, key=lambda v: by_version(v)[0]):
        #print('Major', major_group)
        for minor_group, minor in groupby(major, key=lambda v: by_version(v)[1]):
            #print('Minor', minor_group)
            result[(major_group, minor_group)] = list(minor)
    return result
    
    
lst = ['python3.4.0-foo.gz',
       'python2.4.1-foo.gz',
       'python-3.4.10foo.gz',
       'python-3.4.2-foo.gz',
       'python-3.4.3-foo.gz',
       'python-3.4.4-foo.gz',
       'python-3.4.5-foo.gz',
       'python-3.4.6-foo.gz',
       'python-3.4.7-foo.gz',
       'python-3.4.8-foo.gz',
       'python-3.4.9-foo.gz',
       'python-3.5.0-foo.gz',
       'python-3.5.1-foo.gz',
       'python-3.5.2-foo.gz',
       'python-3.5.3-foo.gz',
       'python-3.5.4-foo.gz',
       'python-3.5.5-foo.gz',
       'python-3.5.6-foo.gz',
       'python-3.5.7-foo.gz']


pprint(sort_by_major_minor(lst))
I did it how I would group Major and Minor Versions together.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
(Jul-12-2019, 04:38 AM)Gribouillis Wrote: Wouldn't sort --version-sort suit your needs?

for a lot uses, perhaps so. but versions is just one example. i illustrated with versions because it was a familiar example. i do have other cases that may or may not work with version handling programs, depending on how those programs extract or parse and deal with numbers. i just don't know what cases these features work with, so i pursue general solutions that i know will or should. for sort --version-sort the man page only describes it for versions.

i remember implementing a general case compare function in 360/370 mainframe assembler language decades ago that worked character by character. it checked for decimal digits and counted how many there were to carry out the comparison as if the two numbers being compared were of equal length. Python posed an interesting difference since it used a function that was to return what was to be compared, unlike, for example, C's qsort() that takes a function that is to perform the actual comparison.

i just found that sort --version-sort changes the collating order of other characters like . and _ which sort higher than alnums with that option. my method does not change the collating order.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 9 455 Mar-29-2024, 06:15 PM
Last Post: idev
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,308 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
Question Numeric Anagrams - Count Occurances monty024 2 1,500 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 1,998 Nov-06-2021, 03:26 PM
Last Post: snippsat
  Extract continuous numeric characters from a string in Python Robotguy 2 2,619 Jan-16-2021, 12:44 AM
Last Post: snippsat
  How to calculate column mean and row skip non numeric and na Mekala 5 4,909 May-06-2020, 10:52 AM
Last Post: anbu23
  Alpha numeric element list search rhubarbpieguy 1 1,771 Apr-01-2020, 12:41 PM
Last Post: pyzyx3qwerty
  convert a character to numeric and back Skaperen 2 2,093 Jan-28-2020, 09:32 PM
Last Post: Skaperen
  are numeric types passed by value or reference? rudihammad 4 2,611 Nov-19-2019, 06:25 AM
Last Post: rudihammad
  'Age' categorical (years -months -days ) to numeric Smiling29 4 2,911 Oct-17-2019, 05:26 PM
Last Post: Smiling29

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020