Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to do a numeric sort
#9
that looks like a good module to have around.

i ended up implementing the numeric sort, for sorting files, this way:

my script acts as a filter, reading stdin then printing the result to stdout.

this filter script defines a special code character as Unicode U+FFF6, very unlikely to be in any text data. if the input line has this code in it, a single split around it is done and the part after the code is printed to stdout. else (if the line does not have the code in it) it does re.split(r'(\d+)',line). then it scans that list for any elements that are .isdecimal(). strings that are .isdecimal() are padded with enough leading zeros to make it a wide fixed width number. the list is then joined back to a single string. the modified string + the code + the original string are then printed to stdout. this filter will undo what it does when it processes its own result. so a Unix shell pipeline like filter|sort|filter performs the numeric sort i want. and it avoids storing the whole file in memory because sometimes i might have millions of lines to sort.



#!/usr/bin/env python3
from re import split
from sys import stdin
code = chr(0xfff6)
width = 25
regexp = r'(\d+)'
pad = '0'*width
for line in stdin:
    line = line.split('\n',1)[0]
    if not line:
        print()
        continue
    if code in line:
        line = line.split(code,1)[1]
        print(line)
        continue
    seq = split(regexp,line)
    for x in range(len(seq)):
        if seq[x].isdecimal():
            seq[x] = (pad+seq[x])[-width:]
    seq.append(code)
    seq.append(line)
    print(''.join(seq))
exit(0)
no, i didn't want to use line.rstrip(). i didn't want to lose any trailing spaces.
Tradition is peer pressure from dead people

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


Messages In This Thread
how to do a numeric sort - by Skaperen - Jul-10-2019, 04:13 AM
RE: how to do a numeric sort - by perfringo - Jul-10-2019, 04:48 AM
RE: how to do a numeric sort - by scidam - Jul-10-2019, 04:54 AM
RE: how to do a numeric sort - by Skaperen - Jul-10-2019, 05:02 AM
RE: how to do a numeric sort - by perfringo - Jul-10-2019, 05:13 AM
RE: how to do a numeric sort - by Skaperen - Jul-10-2019, 05:49 AM
RE: how to do a numeric sort - by perfringo - Jul-10-2019, 07:18 AM
RE: how to do a numeric sort - by Gribouillis - Jul-10-2019, 07:27 AM
RE: how to do a numeric sort - by Skaperen - Jul-12-2019, 02:05 AM
RE: how to do a numeric sort - by Gribouillis - Jul-12-2019, 04:38 AM
RE: how to do a numeric sort - by Skaperen - Jul-12-2019, 09:50 AM
RE: how to do a numeric sort - by DeaD_EyE - Jul-12-2019, 06:13 AM

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