Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naturally sort a list of alpha-numeric tuples by the tuple's first element in Python

A previous stackoverflow question explains how to sort a list of strings alpha-numerically. I would like to sort a list of tuples alphanumerically by the tuple's first element.

Example 1:

>>> sort_naturally_tuple([('b', 0), ('0', 1), ('a', 2)])
[('0', 1), ('a', 2), ('b', 0)]

Example 2:

>>> sort_naturally_tuple([('b10', 0), ('0', 1), ('b9', 2)])
[('0', 1), ('b9', 2), ('b10', 0)]

Update: To emphasize the alphanumeric factor, please review example 2.

like image 429
paragbaxi Avatar asked Dec 27 '22 16:12

paragbaxi


2 Answers

Using the second answer from the other question, generalized to support any method on item as the basis for getting the key:

import re
from operator import itemgetter

def sorted_nicely(l, key):
    """ Sort the given iterable in the way that humans expect."""
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda item: [ convert(c) for c in re.split('([0-9]+)', key(item)) ]
    return sorted(l, key = alphanum_key)


print sorted_nicely([('b10', 0), ('0', 1), ('b9', 2)], itemgetter(0))

This is exactly the same as that answer except generalized to use any callable as the operation on item. If you just wanted to do it on a string, you'd use lambda item: item, if you wanted to do it on a list, tuple, dict, or set, you'd use operator.itemgetter(key_or_index_you_want), or if you wanted to do it on a class instance you could use operator.attrgetter('attribute_name_you_want').

It gives

[('0', 1), ('b9', 2), ('b10', 0)]

for your example #2.

like image 114
agf Avatar answered Dec 30 '22 15:12

agf


Tuples are by default sorted by their elements, starting at the first. So simply do

L = [('b', 0), ('0', 1), ('a', 2)]
L.sort()
print L
# or create a new, sorted list
print sorted([('b', 0), ('0', 1), ('a', 2)])

The question you liked to talks about natural sorting, which is different from normal (alphanumeric) sorting.

Lets say you want to do natural sort on the first item only:

import re
def naturalize(item):
    # turn 'b10' into ('b',10) which sorts correctly
    m = re.match(r'(\w+?)(\d+)', item)
    return m.groups()
# now sort by using this function on the first element of the tuple:
print sorted(L, key=lambda tup: naturalize(tup[0]))
like image 42
Jochen Ritzel Avatar answered Dec 30 '22 15:12

Jochen Ritzel