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!