如何排序 Python 日期字串列表?
在本文中,我們將向您展示如何排序一個 Python 日期字串列表。現在我們看到 3 種方法來完成此任務 -
現在我們看到 2 種方法來完成此任務 -
使用 sort() 和 lambda 函式
使用 sort() 函式
使用 sorted 函式
方法 1:使用 sort() 和 lambda 函式
演算法(步驟)
以下是執行所需任務的演算法/步驟 -
使用 import 關鍵字,從 datetime 模組匯入 datetime(要使用日期和時間,Python 有一個名為 datetime 的模組)。
建立一個變數來儲存輸入的日期字串列表
使用 sort() 函式 透過將引數作為 lambda 函式來對日期列表進行排序。在這裡,在 lambda 函式中,我們使用 strptime() 函式(將字串格式的時間戳格式化為日期時間物件)將每個日期轉換為日期物件。
列印排序後的輸入日期字串列表。
示例
以下程式使用 datetime 模組返回輸入日期字串列表的排序列表 -
# importing datetime from datetime import datetime # input list of date strings inputDateList = ['06-2014', '08-2020', '4-2003', '04-2005', '10-2002', '12-2021'] # sorting the input list by formatting each date using the strptime() function inputDateList.sort(key=lambda date: datetime.strptime(date, "%m-%Y")) # Printing the input list after sorting print("The input list of date strings after sorting:\n", inputDateList)
輸出
執行上述程式後,將生成以下輸出 -
The input list of date strings after sorting: ['10-2002', '4-2003', '04-2005', '06-2014', '08-2020', '12-2021']
方法 2:使用 sort() 函式
sort() 方法就地對原始列表進行排序。這意味著 sort() 方法會更改列表元素的順序。
預設情況下,sort() 方法使用小於運算子 (<) 對列表的條目進行排序,即按 **升序** 排序。換句話說,它優先考慮較小的元素而不是較大的元素。
要按從高到低(**降序**)排序元素,請在 sort() 方法中使用 reverse=True 引數。
list.sort(reverse=True)
演算法(步驟)
以下是執行所需任務的演算法/步驟 -
建立一個變數來儲存輸入的日期字串列表
建立一個接受輸入列表作為引數的函式。
使用 split() 函式(將字串拆分為列表。我們可以定義分隔符;預設分隔符是任何空格),根據 '-' 分隔符拆分元素列表。
使用 sort() 函式,透過將鍵作為函式名作為引數傳遞給它來對輸入日期字串列表進行排序。
列印排序後的輸入日期字串列表。
示例
以下程式使用 sort() 函式返回輸入日期字串列表的排序列表 -
# input list of date strings inputDateList = ['06-2014', '08-2020', '4-2003', '04-2005', '10-2002', '12-2021'] # creating a function that accepts the input list as an argument def sortDates(datesList): # splitting the list of elements based on the '-' separator(as the date is separated by - symbol ex 06-2014) split_up = datesList.split('-') # returning the year, the month of input list elements # Here split_up[1] gives the year and split_up[0] gives month return split_up[1], split_up[0] # sorting the input list of date strings using the sort() function # here the key is the function name inputDateList.sort(key=sortDates) # Printing the input list after sorting print("The input list of date strings after sorting:\n", inputDateList)
輸出
The input list of date strings after sorting: ['10-2002', '4-2003', '04-2005', '06-2014', '08-2020', '12-2021']
我們以日期列表作為輸入,然後編寫了一個函式,該函式以列表元素作為引數(日期),然後將日期拆分為單獨的元素月和年並返回它。我們在給定列表上使用了 sort() 函式並將函式名作為引數傳遞。結果 sort() 函式將函式應用於每個元素並相應地對其進行排序。
方法 3:使用 sorted() 函式
sorted() 函式返回給定可迭代物件的排序列表。
您可以選擇升序或降序。數字按數字排序,而字串按字母順序排列。
語法
sorted(iterable, key=key, reverse=reverse)
引數
iterable - 它是一個序列。
key - 將執行以確定順序的函式。預設值為 None。
reverse - 布林表示式。True 按升序排序,False 按降序排序。預設值為 False。
演算法(步驟)
使用 sorted() 函式,透過將列表、鍵作為函式名作為引數傳遞給它來對輸入日期字串列表進行排序,並列印輸出
示例
以下程式使用 sorted 函式返回輸入日期字串列表的排序列表 -
inputDateList = ['06-2014', '08-2020', '4-2003', '04-2005', '10-2002', '12-2021'] def sortDates(datesList): # splitting the list of elements based on the '-' separator split_up = datesList.split('-') # returning the year, the month of input list elements # Here split_up[1] gives the year and split_up[0] gives month return split_up[1], split_up[0] print("The input list of date strings after sorting:\n") # sorting the input list of date strings using the sorted function # here the key is the function name print(sorted(inputDateList, key=sortDates))
輸出
The input list of date strings after sorting: ['10-2002', '4-2003', '04-2005', '06-2014', '08-2020', '12-2021']
它與之前的方法相同,只是 sorted() 函式接受輸入列表和函式作為引數。在這種情況下,我們將鍵用作函式名,因為 sorted() 函式獲取每個日期,將其拆分,並根據它進行排序。
結論
在本文中,我們學習瞭如何使用三種不同的方法對日期列表進行排序。我們還學習瞭如何使用 split() 函式將給定日期劃分為月和年。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP