Sort dict to list

I have it:

dictionary = { (month, year) : [int, int, int] }

I would like to get a list of tuples / lists with ordered data (by month and year):

#example info
list = [(8,2010,2,5,3),(1,2011,6,7,8)...]

I tried several times, but I can not find a solution.

Thank you for your help.

+3
source share
3 answers

Do not use the built-in names as an identifier - this is a terrible practice, without any advantages, and ultimately it will lead you to some kind of bad behavior. So I call the result thelist(arbitrary, anodized, only thin identifier), not list (inline shading).

import operator

thelist = sorted((my + tuple(v) for my, v in dictionary.iteritems()),
                 key = operator.itemgetter(1, 0))
+5
source

Something like this should be done:

>>> d = { (8, 2010) : [2,5,3], (1, 2011) : [6,7,8], (6, 2010) : [11,12,13] }
>>> sorted((i for i in d.iteritems()), key=lambda x: (x[0][1], x[0][0]))
[((6, 2010), [11, 12, 13]), ((8, 2010), [2, 5, 3]), ((1, 2011), [6, 7, 8])]

(, , .)

, itemgetter , . Alex Martelli.

0

, .

l = [(m, y) + tuple(d[(y, m)]) for y, m in sorted(d)]
0
source

Source: https://habr.com/ru/post/1762966/


All Articles