mik.ferrucci mik.ferrucci - 1 month ago 6
Python Question

Sorting dict items by an integer key, ad hoc function

I have a question similar to that one I asked last week:
"Sorting dict items by key, beyond alphanumeric sorting"

The problem is identic, but now the keys of my dict are simply integers:

>>>lis_nodiz = list(nodiz.items()) #list of dict items as tuples
[(2, 316),
(3, 66),
(4, 37),
(5, 15),
(6, 10),
(7, 4),
(8, 3),
(9, 1),
(10, 2),
(11, 1),
(12, 1),
(45, 1),
(109, 1),
(16, 1),
(126, 1)]


As you would have guess I'm searching for this ordered list of tuples:

[(2, 316),
(3, 66),
(4, 37),
(5, 15),
(6, 10),
(7, 4),
(8, 3),
(9, 1),
(10, 2),
(11, 1),
(12, 1),
(16, 1),
(45, 1),
(109, 1),
(126, 1)]


I have tried to reuse the "key_func" Padraic Cunningham suggested me, unfortunately this function seems to work only with string type dict keys:

def key_func(x):
"""'a0p12' -> (0, 12)"""
return tuple(int("".join(v)) for k,v in groupby(x[0], key=str.isdigit) if k)

>>>lis_nodiz_od = sorted(nodiz.items(), key=key_func)
TypeError: expected string or buffer


How to modify the function, or how can I do in another way to get my purpose?
I would appreciate very much if somebody explains how the "key_func" really works, and the correct modifications to do in order to make it work with integers; this way I will understand more about Python programming I hope, and not only solve my problem of today!
Thanks you all very much!

Answer

Call sorted() or list.sort() as:

# sorted(): Creates new list with sorted order
>>> sorted(lis_nodiz)  
[(2, 316), (3, 66), (4, 37), (5, 15), (6, 10), (7, 4), (8, 3), (9, 1), (10, 2), (11, 1), (12, 1), (16, 1), (45, 1), (109, 1), (126, 1)]

# list.sort(): sort the existing  list
>>> lis_nodiz.sort()
>>> lis_nodiz
[(2, 316), (3, 66), (4, 37), (5, 15), (6, 10), (7, 4), (8, 3), (9, 1), (10, 2), (11, 1), (12, 1), (16, 1), (45, 1), (109, 1), (126, 1)]

where lis_nodiz is list of tuples as mentioned in question.

Note: No need to specify key here. By default does lexicographical sort on 0th index; in case of same value, sorts on 1st index.