Python Forum
sorting alphanumeric values in a human way
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting alphanumeric values in a human way
#3
Here's one option :

from itertools import groupby

def keyfunction (string) :
	return [int (''.join (group)) if key else ''.join (group) for key, group in groupby (string, str.isdigit)]

test_list = ['A35','A4', 'B3', 'A1', 'A15']

print (sorted (test_list, key = keyfunction)) 
If you can and want to install natsort with pip, here's another option :

import natsort

test_list = ['A35','A4', 'B3', 'A1', 'A15']
print (natsort.natsorted (test_list))
Reply


Messages In This Thread
RE: sorting alphanumeric values in a human way - by BashBedlam - Jan-22-2021, 05:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Human Sorting (natsort) does not work [SOLVED] AlphaInc 2 1,182 Jul-04-2022, 10:21 AM
Last Post: AlphaInc
  Sorting numerical values provided by QAbstractTableModel BigMan 0 1,390 Jun-04-2022, 12:32 AM
Last Post: BigMan
  List sort - alphanumeric? papsphilip 2 2,505 Oct-05-2019, 09:27 PM
Last Post: papsphilip
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 3,125 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER
  How to convert Python crawled Bing web page content to human readable? dalaludidu 4 3,465 Sep-02-2018, 04:15 PM
Last Post: dalaludidu
  Batch job from epoch to human time jheeman 6 4,582 Feb-27-2018, 10:53 PM
Last Post: jheeman
  Time Difference in Epoch Microseconds then convert to human readable firesh 4 11,678 Feb-27-2018, 09:08 AM
Last Post: firesh
  Sorting values calculated in python stumunro 4 4,020 Sep-13-2017, 06:09 AM
Last Post: nilamo
  sorting nested dict according to values merlem 6 17,697 Apr-01-2017, 10:01 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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