How to sort 2D arrays, dictionaries and lists of proprietary classes

Sorting a two-dimensional array

>>> hoge = [[10,4],[4,9],[5,0],[4,5],[2,0],[3,5]]
>>> sorted(hoge)
[[2, 0], [3, 5], [4, 5], [4, 9], [5, 0], [10, 4]]
>>> sorted(hoge, key=lambda x:x[1])
[[5, 0], [2, 0], [10, 4], [4, 5], [3, 5], [4, 9]]    #Sort by second element
>>> hoge
[[10, 4], [4, 9], [5, 0], [4, 5], [2, 0], [3, 5]]    #Sorted does not change the original array
>>> hoge.sort(key=lambda x:x[0])
>>> hoge
[[2, 0], [3, 5], [4, 9], [4, 5], [5, 0], [10, 4]]    #When sorting by the first element[4, 9], [4, 5]The order of
>>> hoge.sort()
>>> hoge
[[2, 0], [3, 5], [4, 5], [4, 9], [5, 0], [10, 4]]    #If you sort by all elements[4, 5], [4, 9]The order of

Sort dictionary by key

>>> d = {'Yamada': 100, 'Sato': 50, 'Kobayashi': 75}
>>> sorted( d.items() )
[('Kobayashi', 75), ('Sato', 50), ('Yamada', 100)]

Sort dictionary by value

>>> sorted(d.items(), key=lambda x:x[1])
[('Sato', 50), ('Kobayashi', 75), ('Yamada', 100)]

Sort the list of your own classes

Preparation

>>> class Seiseki:
...     def __init__(self, name, tensu):
...             self.name = name
...             self.tensu = tensu
...     def output(self):
...             print("Name:{0}, Tensu:{1}".format(self.name, self.tensu))
... 
>>> ll = [Seiseki('Yamada', 100), Seiseki('Sato', 50), Seiseki('Kobayashi', 75)]
>>> for l in ll:
...     l.output()

Name:Yamada, Tensu:100
Name:Sato, Tensu:50
Name:Kobayashi, Tensu:75

sort by name

>>> ll.sort(key=lambda x: x.name)
>>> for l in ll:
...     l.output()
... 
Name:Kobayashi, Tensu:75
Name:Sato, Tensu:50
Name:Yamada, Tensu:100

sort by tensu

>>> ll.sort(key=lambda x: x.tensu)
>>> for l in ll:
...     l.output()
... 
Name:Sato, Tensu:50
Name:Kobayashi, Tensu:75
Name:Yamada, Tensu:100

Recommended Posts

How to sort 2D arrays, dictionaries and lists of proprietary classes
How to use lists, tuples, dictionaries, and sets
[python] Summary of how to retrieve lists and dictionary elements
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
About cumulative assignment of lists and numpy arrays
[Python] How to sort dict in list and instance in list
Save lists, dictionaries and tuples to external files python
Foreigners talk: How to name classes and methods in English
[Python] Summary of how to use split and join functions
Comparison of how to use higher-order functions in Python 2 and 3
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
Introduction of DataLiner ver.1.3 and how to use Union Append
Overview of Python virtual environment and how to create it
python> Handling of 2D arrays
Differentiation of sort and generalization of sort
Overview of how to create a server socket and how to establish a client socket
How to compare lists and retrieve common elements in a list
[Python] How to get the first and last days of the month
I summarized how to change the boot parameters of GRUB and GRUB2
Summary of how to use pandas.DataFrame.loc
How to use classes in Theano
Summary of how to use pyenv-virtualenv
How to install and configure blackbird
How to use .bash_profile and .bashrc
How to install and use Graphviz
I'm addicted to Python 2D lists
Summary of how to use csvkit
How to solve slide puzzles and 15 puzzles
[Python] How to specify the window display position and size of matplotlib
I want to sort a list in the order of other lists
Introduction of cyber security framework "MITRE CALDERA": How to use and training
[Python --open3d] How to convert obj data of 3D model to point cloud