程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Example of sorted() function in Python

編輯:Python

一個強大的Python內置函數是sorted()函數,It is great for work with data sequences.This function accepts a sequence as required input,Can also receive an optional second keyword argument,名為reverse.反向 "參數可以被設置為 "真 "或 "假".If not specified in the function call,'reverse'默認設置為False,Means to use ascending order in the sort operation.If what you're looking for is descending order,Then set the reverse to True.sorted()The function returns a new sorted list from the items in the given sequence.It does not modify the given sequence itself,This is a key point to understand,因為在 Python There are other sorting methods in ,Data can be sorted in-place.We can take a look at it in this tutorialPython sorted()A few examples of functions.


Sorts strings alphabetically in ascending order

The first sorting example we can look at is a list of strings.So let's populate a list with the names of some famous guitarists.

Now let's callsorted()函數並傳入'guitarists'變量.We store the result of this function call in a file named 'guitarists_sorted_a'的新變量中.This new variable now holds the original'guitarists'The ascending alphabetical version of the variable.

sorted()A key point of the function is,It doesn't modify the original iterable variable.in order to get a sorted version of the iterator,You can call sorted() The return value of is stored in a new variable,就像我們上面看到的那樣.


Sorts strings in descending alphabetical order

sorted()The function has an optional second parameter,名為'reverse',you can pass it.We can use the value as True的'reverse'關鍵字,Sort our guitarist list in descending alphabetical order.

再一次,The original list has not been modified.


Sort numbers in a tuple

現在,Let's play with some numbers sorted() 函數.We'll add one first'numbers'變量,and store a tuple of some random integer values.

現在我們可以調用 sorted() 函數,並傳入'numbers'變量.The result of this function call is stored in a file named 'numbers_sorted_a'的新變量中.輸出顯示,The new variable now has integers sorted from lowest value to highest value.

Like in our string example,The original iterator is not modified.'numbers'The variables are still the same as before.

Our goal now is to sort the tuple of numbers in descending order.We can use our convenient'reverse'參數設置為True來完成這個任務.


Sort floats in a list

在sorted()function in this example,We have a list of floating point numbers.

現在我們可以通過調用sorted()函數並傳入'floats'variable to get a sorted version of this list.The result is a list of floating point numbers sorted in ascending order.

We still remember howsorted()中使用'reverse',So let's use it in this list of floats too.


使用sorted()with a key

There is also a third parameter that can be used with sorted() 一起使用,那就是'key'參數.它是一個函數,as the key to sorting comparisons.在下一個例子中,We start with a list of primitives.Each tuple has two values.when we use this data structure sorted() 時,It is sorted by the first value in the tuple.

What if we wanted to sort the list of tuples by the second value instead of the first?我們可以使用 key 函數來實現這一點.


Python sorted() 函數總結

Python sorted() The function returns a sorted list from the items contained in an iterator.The resulting sort is ascending by default,除非你提供'reverse=True'參數,在這種情況下,Sorting is descending.

sorted() 函數的語法如下:

sorted(iterable, key=None, reverse=False)
復制代碼

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved