When specifying multiple keys in python sort

I will not touch on the difference between list.sort () and sorted (). It seems that you can write like this about the specification of the key to sort. 2.7, maybe 3 and later.

python2.7


from pprint import pprint

items = [
  {'id':2, 'book': {'type':'zassi', 'name': u'bbb' }},
  {'id':3, 'book': {'type':'manga', 'name': u'ccc' }},
  {'id':4, 'book': {'type':'zassi', 'name': u'ddd' }},
  {'id':1, 'book': {'type':'manga', 'name': u'aaa' }},
  {'id':5, 'book': {'type':'manga', 'name': u'eee' }},
]

#One sort key
sorted_items = sorted(
  items,
  key = lambda x: x['id']
)
pprint(sorted_items)

#=>
#[{'book': {'name': u'aaa', 'type': 'manga'}, 'id': 1},
# {'book': {'name': u'bbb', 'type': 'zassi'}, 'id': 2},
# {'book': {'name': u'ccc', 'type': 'manga'}, 'id': 3},
# {'book': {'name': u'ddd', 'type': 'zassi'}, 'id': 4},
# {'book': {'name': u'eee', 'type': 'manga'}, 'id': 5}]


#Two or more sort keys
sorted_items = sorted(
  items,
  key = lambda x: (x['book']['type'], x['id'])
)
pprint(sorted_items)

#=>
#[{'book': {'name': u'aaa', 'type': 'manga'}, 'id': 1},
# {'book': {'name': u'ccc', 'type': 'manga'}, 'id': 3},
# {'book': {'name': u'eee', 'type': 'manga'}, 'id': 5},
# {'book': {'name': u'bbb', 'type': 'zassi'}, 'id': 2},
# {'book': {'name': u'ddd', 'type': 'zassi'}, 'id': 4}]

I wrote later and noticed that item getter and attr getter seem to only look at the same hierarchy, so for example, [{book: {}, shelf: {}}, {book: {}, shelf: {} }] With a structure like, I feel that it is not possible to specify keys like this for books and this for shelves. </ del>-> You can specify these with tuples without using them.

Recommended Posts

When specifying multiple keys in python sort
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
Bubble sort in Python
Custom sort in Python3
Get multiple maximum keys in Python dictionary type
Multiple regression expressions in Python
Naturally sort Path in Python
python in mongodb in descending sort
Attention when os.mkdir in Python
Avoid multiple loops in Python
Prohibit multiple launches in python
Sort by date in python
Extract multiple list duplicates in Python
[Python] Show multiple windows in Tkinter
Precautions when using pit in Python
[Python] Sort iterable by multiple conditions
Behavior when listing in Python heapq
Sort large text files in Python
Statistical test (multiple test) in Python: scikit_posthocs
Delete multiple elements in python list
Sort by specifying conditions in CASTable
When using regular expressions in Python
When writing a program in Python
Be careful when specifying the default argument value in Python3 series
[Python] Sort
Python # sort
Handle multiple python versions in one jupyter
New in Python 3.9 (2)-Sort directed acyclic graphs in Python
Create ScriptableObject in Python when building ADX2
Precautions when pickling a function in python
Send email to multiple recipients in Python (Python 3)
When looking at memory usage in Python 3
Process multiple lists with for in Python
Implemented Stooge sort in Python3 (Bubble sort & Quicksort)
Speed comparison when searching with multiple keys in pandas (MultiIndex vs Others)
How to write a string when there are multiple lines in python
How to sort by specifying a column in the Python Numpy array.
[Tips] Easy-to-read writing when connecting functions in Python
Quadtree in Python --2
When codec can't decode byte appears in python
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
[Python] Sort the list of pathlib.Path in natural sort
How to take multiple arguments when doing parallel processing using multiprocessing in python
Meta-analysis in Python
Unittest in python
When I try matplotlib in Python, it says'cairo.Context'
Execution order when multiple context managers are specified in the Python with statement
Epoch in Python
Discord in Python
[CpawCTF] Q14. [PPC] Try writing Sort! In Python
Sudoku in Python
DCI in Python
quicksort in python
A memo about writing merge sort in Python
Precautions when dealing with control structures in Python 2.6
nCr in python