Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use sort, sorted and argsort of python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to use python's sort, sorted and argsort". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use python's sort, sorted and argsort" can help you solve your doubts.

Introduction

These three sorting methods are basically sufficient for daily work.

Let's talk about the difference between the three first.

Sort, sorted is a sort method used in list data types

Argsort is a sort method used in numpy data types (there is also a sort method in numpy, which will be discussed below)

The differences between sort and sorted are as follows

First look at two simple ascending sorting, using the sorted and sort methods, respectively.

# sorted num_list = [1,8,2,3,10,4,5] ordered_list = sorted (num_list) print (ordered_list) # [1,2,3,4,5,8,10] # sortnum_list = [1,8,2,3,10,4,5] num_list.sort () print (num_list) # [1,2,3,4,5,8,10]

You can see that sorted does not modify the original array, but passes the sorted result as a parameter to a new array, while sort sorts directly on the original array.

The difference is that sorted needs a variable to receive the sort results, but sort does not

It is recommended to use sorted, because although the sort code is more concise, it will modify the original array, so it is not flexible. If you use this array in multiple places at the same time, then the array after sort operation is no longer the original array.

Debug is very troublesome. After talking about the difference, let's talk about how to use it.

Usage example 1. Ascending sort # sorted ascending sort num_list = [1, 8, 2, 3, 10, 4, 5] ordered_list = sorted (num_list) print (ordered_list) # [1, 2, 3, 4, 5, 8, 10] # sort ascending sort num_list = [1, 8, 2, 3, 3, 3, 10, 5] num_list.sort () print (num_list) # [1, 2, 3, 4, 5, 8, 10] 2. Descending sort # sorted descending sort num_list = [1, 8, 2, 3, 10, 4, 5] ordered_list = sorted (num_list, reverse=True) print (ordered_list) # [1, 2, 3, 4, 5, 8, 10] # sort descending sort num_list = [1, 8, 2, 3, 3, 10, 4, 5] num_list.sort (reverse=True) print (num_list) # [1, 2, 3, 4, 5, 8, 10] 3. If you don't want the sorted value and want the sorted index, you can do this num_list = [1, 8, 2, 3, 10, 4, 5] ordered_list = sorted (range (len (num_list)), key=lambda k: num_ list [k]) print (ordered_list) # [0, 2, 3, 5, 6, 1,4] 4. String type sort # string type sort str_list = ['1times,' 8levels, '2orders,' 3bands, '10bands,' 4bands,'5'] ordered_list = sorted (str_list) print (ordered_list) # ['1bands,' 10bands, '2bands,' 3orders, '4strings,' 5bands,'8'] str_list = ['Aids,' Downs,'B'' Ordered_list = sorted (str_list) print (ordered_list) # [A, B, C, D, N, R, V] 5. The two-dimensional array is sorted by book_list = [['study of Marxism at Peking University', '9787509728529', 2011], ['Human Liberation', '9787215064003', 2014], ['Western Classical Reading Capital', '9787200092882', 2012], ['Life of Lenin', '9787501319343',] # sorted in ascending order of publication year ordered_list = sorted (book_list) Key=lambda book: book [2]) print (ordered_list) # [['Research on Marxism of Peking University', '9787509728529', 2011], ['Western Classical Reading Capital', '9787200092882', 2011], ['Life of Lenin', '9787501319343', 2013], ['Liberation of Man', '9787215064003] # sort sorted by year of publication book_list.sort (key=lambda book: book [2]) Reverse=True) print (book_list) # [['Liberation of Man', '9787215064003, 2014], [' Life of Lenin', '9787501319343, 2013], [' Western Classical Reading Capital', '9787200092882, 2012], [' Research on Marxism of Peking University', '978750928529] 6. Two-dimensional array gets the sorted index # sorted gets the sorted index book_list = [['Research on Marxism of Peking University', '9787509728529, 2011], [' Liberation of Man', '9787215064003, 2011], [' Western Classical Reading Capital', '9787200092882, 2012], [' Life of Lenin', '97875013193431934],] ordered_list = sorted (len (book_list)) Key=lambda k: book_ list [k] [2]) print (ordered_list) # [0,2,3,1] 7. Dictionary array order book_list = [{'name':' Peking University Marxist Studies', 'isbn':' 9787509728529, 'publish_year': 2011}, {' name': 'Human Liberation', 'isbn':' 9787215064003}, {'name':' Western Classical Reading Capital', 'isbn':' 97872000928882, 'publish_year': 2012} {'name':' the Life of Lenin, 'isbn':' 9787501319343, 'publish_year': 2013},] # sorted in descending order of publication ordered_list = sorted (book_list, key=lambda book: book [' publish_year'], reverse=True) print (ordered_list) # [{'name':' Human Liberation', 'isbn':' 9787215064003, 'publish_year': 2014}, {' name': 'Life of Lenin' 'isbn':' 9787501319343, 'publish_year': 2013}, {' name': 'Western Classics Reading Capital,' isbn': '9787200092882,' publish_year': 2012}, {'name':' Marxist Studies of Peking University, 'isbn':' 9787509728529' 'publish_year': 2011}] # sort in ascending order of publication book_list.sort (key=lambda book: book [' publish_year']) print (book_list) # [{'name':' study of Marxism at Peking University', 'isbn':' 9787509728529, 'publish_year': 2011}, {' name': 'Western Classical Reading Capital', 'isbn':' 9787200092882), 'publish_year': 2012} {'name':' Lenin's Life, 'isbn':' 9787501319343, 'publish_year': 2013}, {' name': 'Human Liberation', 'isbn':' 9787215064003, 'publish_year': 2014}] 8. The index book_list = [{'name':' Marxist study of Peking University', 'isbn':' 9787509728529, 'publish_year': 2011}, {' name': 'human liberation', 'isbn':' 9787215064003, 'publish_year': 2014}, {' name': 'Western Classical Reading Capital', 'isbn':' 9787200092882' 'publish_year': 2012}, {' name': 'Lenin's Life', 'isbn':' 9787501319343, 'publish_year': 2013},] ordered_list = sorted (range (len (book_list)), key=lambda k: book_ list [k] [' publish_year']) print (ordered_list) # [0,2,3,1] 9. Object sort class Book (object): def _ init__ (self, name, isbn, publish_year): self.name = name self.isbn = isbn self.publish_year = publish_year def _ repr__ (self): return repr ((self.name, self.isbn, self.publish_year)) book_list = [Book ('Marxist Studies of Peking University', '9787509728529, 2011) Book ('Liberation of Man', '9787215064003, 2014), Book (' Western Classical Reading Capital', '9787200092882, 2012), Book (' Life of Lenin', '9787501319343, 2013),] # sorted, in descending order of publication, ordered_list = sorted (book_list, key=lambda book: book.publish_year, reverse=True) print (ordered_list) # [(' Liberation of Man, '9787215064003) 9787501319343, 2013), ('Western Classical Reading Capital', '9787200092882 Capital',), ('Peking University Marxism Studies', '9787509728529')] # sort in ascending order of publication book_list.sort (key=lambda book: book.publish_year) print (book_list) # [('Peking University Marxism Studies', '9787509728529 Capital', '97872000928882 Capital', 9787200092882) (life of Lenin, 9787501319343, 2013), (Liberation of Man, 9787215064003)] 10. The sorted index book_list = [Book ('study of Marxism of Peking University', '9787509728529, 2011), Book (' Liberation of Man', '9787215064003), Book (' Western Classical Reading Capital', '9787200092882), Book (' Life of Lenin', '9787501319343),] ordered_list = sorted (len (book_list)) Key=lambda k: book_ list [k]. Publish _ year) print (ordered_list) # [0,2,3,1] 11. One-dimensional array sorting [numpy]

Numpy has only sort but no sorted, and the sort method of numpy is similar to the sorted method of list

Import numpy as np# one-dimensional array num_list = np.array ([1, 8, 2, 3, 10, 4, 5]) index_list = np.sort (num_list) print (index_list) # [12 3 4 58 10] 12. One-dimensional array gets the sorted index [numpy] num_list = np.array ([1, 8, 2, 3, 10, 4, 5]) index_list = np.argsort (num_list) print (index_list) # [0 23 56 14] 13. One-dimensional array sort in descending order [numpy] # # sort num_list = np.array ([1, 8, 2, 3, 10, 4, 5]) index_list = np.argsort (- num_list) # plus a minus sign sort print (index_list) # [4 1653 20] 14. Two-dimensional array sort [numpy] num_list = np.array ([[1, 8, 2, 9], [8, 2, 4, 5], [2, 3, 7, 4], [1, 2, 3, 5]) ordered_list = np.sort (num_list Axis=0) # axis=0 is sorted by column print (ordered_list) # [[1 2 24] # [1 23 5] # [2 3 4 5] # [8 8 7 9]] ordered_list = np.sort (num_list, axis=1) # axis=1 is sorted by row print (ordered_list) # [[1 28 9] # [2 458] # [2 3 4 7] # [1 2 3 5]] 15. Get the sorted index [numpy] num_list = np.array ([[1, 8, 2, 9], [8, 2, 4, 5], [2, 3, 7, 4], [1, 2, 3, 5]]) ordered_list = np.argsort (num_list) Axis=0) # axis=0 is sorted by column print (ordered_list) # [[0 102] # [3 331] # [22 13] # [1 020]] ordered_list = np.argsort (num_list, axis=1) # axis=1 is sorted by row print (ordered_list) # [[0 21 3] # [1 230] # [0 13 2] # [0 1 23] attached: python sorts the array And output the corresponding index value #-*-coding: cp936-*-import numpy as np# one-dimensional array sort arr = [1, 3, 5, 2, 4, 6] arr = np.array (arr) print arrprint np.sort (arr) # or print np.sort (arr,axis=None) print (np.argsort (arr)) # positive output index, print (np.argsort (- arr)) # output index in reverse order from small to large

Output result:

[1 3 5 2 4 6]

[1 2 3 4 5 6]

[0 3 1 4 2 5]

[5 2 4 1 3 0]

# two-dimensional array sort list1 = [[4je 3jue 2], [2je 1je 4]] array=np.array (list1) print arrayarray.sort (axis=1) # axis=1 sort by row, axis=0 sort by column print array

Output result:

[[4 3 2]

[2 1 4]]

[[2 3 4]

[1 2 4]]

After reading this, the article "how to use sort, sorted and argsort of python" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report