Size: 579
Comment: Question: This a good way?
|
Size: 847
Comment: add name of pattern, reformat code and rename some variables
|
Deletions are marked like this. | Additions are marked like this. |
Line 11: | Line 11: |
a = { "key1":5, "key2":8, "key3":2 } b = { "key1":7, "key2":4, "key3":9 } c = { "key1":6, "key2":1, "key3":1 } l = [ a,b,c ] # how do you sort this list? |
a = {"key1": 5 , "key2": 8, "key3": 2} b = {"key1": 7 , "key2": 4, "key3": 9} c = {"key1": 6 , "key2": 1, "key3": 1} undecorated = [a, b, c] # how do you sort this list? |
Line 17: | Line 17: |
There are many ways to do this; Here's one way I've been doing it: | There are many ways to do this. Here's the fastest way to do it, as it avoids using a custom comparison function, instead using builtin comparisons. This is the ''decorate-sort-undecorate'' (DSU) pattern, or the ''Schwartzian transform'' if you're coming from Perl. |
Line 21: | Line 21: |
sort_key = "key2" l = [(d[sort_key],d) for d in l] l.sort() l = [d for (k,d) in l] |
sort_on = "key2" decorated = [(dict_[sort_on], dict_) for dict_ in undecorated] decorated.sort() sorted = [dict_ for (key, dict_) in l] |
Sorting Lists of Dictionaries
Frequently you want to sort a list of dictionaries, based on some particular key.
For example:
Toggle line numbers
1 a = {"key1": 5 , "key2": 8, "key3": 2}
2 b = {"key1": 7 , "key2": 4, "key3": 9}
3 c = {"key1": 6 , "key2": 1, "key3": 1}
4 undecorated = [a, b, c] # how do you sort this list?
There are many ways to do this. Here's the fastest way to do it, as it avoids using a custom comparison function, instead using builtin comparisons. This is the decorate-sort-undecorate (DSU) pattern, or the Schwartzian transform if you're coming from Perl.
Toggle line numbers
1 sort_on = "key2"
2 decorated = [(dict_[sort_on], dict_) for dict_ in undecorated]
3 decorated.sort()
4 sorted = [dict_ for (key, dict_) in l]
Questions
This a good way? -- LionKimbro