AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當前位置:首頁 > 科技  > 軟件

深入探索Python排序神器:sorted()函數(shù)全解析

來源: 責編: 時間:2024-06-21 17:23:42 136觀看
導讀在Python編程領(lǐng)域,sorted()函數(shù)作為數(shù)據(jù)排序的核心工具,憑借其靈活性和高效性,成為了每個開發(fā)者手中的必備神器。本文將帶你全面了解sorted()函數(shù)的使用方法、高級技巧及實際應用,通過超過10個生動的代碼示例,深度挖掘這一

在Python編程領(lǐng)域,sorted()函數(shù)作為數(shù)據(jù)排序的核心工具,憑借其靈活性和高效性,成為了每個開發(fā)者手中的必備神器。本文將帶你全面了解sorted()函數(shù)的使用方法、高級技巧及實際應用,通過超過10個生動的代碼示例,深度挖掘這一功能的強大之處。ZJg28資訊網(wǎng)——每日最新資訊28at.com

ZJg28資訊網(wǎng)——每日最新資訊28at.com

簡介:sorted()函數(shù)初探

sorted()函數(shù)是一種內(nèi)置的高級排序方法,能夠?qū)θ魏慰傻鷮ο螅ㄈ缌斜怼⒃M、字符串等)進行排序,返回一個新的排序后的列表,原對象保持不變。其基本語法為:sorted(iterable[, key][, reverse]),其中:ZJg28資訊網(wǎng)——每日最新資訊28at.com

iterable 是待排序的可迭代對象。ZJg28資訊網(wǎng)——每日最新資訊28at.com

key 是一個可選參數(shù),用于指定一個函數(shù)來作為排序的依據(jù)。ZJg28資訊網(wǎng)——每日最新資訊28at.com

reverse 也是一個可選參數(shù),布爾值,默認為False,表示升序排列;設(shè)為True則為降序排列。ZJg28資訊網(wǎng)——每日最新資訊28at.com

基礎(chǔ)應用:純數(shù)據(jù)排序

示例1:簡單列表排序ZJg28資訊網(wǎng)——每日最新資訊28at.com

numbers = [3, 1, 4, 1, 5, 9, 2, 6]sorted_numbers = sorted(numbers)print(sorted_numbers)  # 輸出:[1, 1, 2, 3, 4, 5, 6, 9]

示例2:字符串排序ZJg28資訊網(wǎng)——每日最新資訊28at.com

words = ["banana", "apple", "cherry"]sorted_words = sorted(words)print(sorted_words)  # 輸出:['apple', 'banana', 'cherry']

高級技巧:利用key參數(shù)定制排序規(guī)則

示例3:按字符串長度排序ZJg28資訊網(wǎng)——每日最新資訊28at.com

fruits = ["apple", "banana", "cherry", "date"]sorted_by_length = sorted(fruits, key=len)print(sorted_by_length)  # 輸出:['date', 'apple', 'cherry', 'banana']

示例4:按絕對值排序負數(shù)ZJg28資訊網(wǎng)——每日最新資訊28at.com

nums = [-5, -3, 2, 4, -1]sorted_abs = sorted(nums, key=abs)print(sorted_abs)  # 輸出:[-1, 2, -3, 4, -5]示例5:按姓名的姓氏排序people = ["Alice Johnson", "Bob Smith", "Charlie Brown"]sorted_by_last_name = sorted(people, key=lambda name: name.split()[-1])print(sorted_by_last_name)  # 輸出:['Charlie Brown', 'Alice Johnson', 'Bob Smith']

逆向排序:掌握reverse參數(shù)

示例6:降序排列整數(shù)ZJg28資訊網(wǎng)——每日最新資訊28at.com

numbers_desc = sorted([8, 3, 1, 6, 4], reverse=True)print(numbers_desc)  # 輸出:[8, 6, 4, 3, 1]

示例7:字符串倒序排列ZJg28資訊網(wǎng)——每日最新資訊28at.com

words_desc = sorted(["hello", "world", "python"], reverse=True)print(words_desc)  # 輸出:['python', 'world', 'hello']

復合排序:結(jié)合多個條件

示例8:先按長度后按字母順序排序ZJg28資訊網(wǎng)——每日最新資訊28at.com

items = ["apple", "banana", "pear", "orange"]sorted_complex = sorted(items, key=lambda x: (len(x), x))print(sorted_complex)  # 輸出:['pear', 'apple', 'orange', 'banana']

實戰(zhàn)應用:sorted()在數(shù)據(jù)處理中的妙用

示例9:排序字典列表的某個字段ZJg28資訊網(wǎng)——每日最新資訊28at.com

students = [    {"name": "Tom", "grade": 88},    {"name": "Jerry", "grade": 92},    {"name": "Spike", "grade": 76}]sorted_students = sorted(students, key=lambda student: student["grade"], reverse=True)print(sorted_students)# 輸出:[{'name': 'Jerry', 'grade': 92}, {'name': 'Tom', 'grade': 88}, {'name': 'Spike', 'grade': 76}]

示例10:統(tǒng)計詞頻并排序ZJg28資訊網(wǎng)——每日最新資訊28at.com

from collections import Countertext = "the quick brown fox jumps over the lazy dog"words = text.split()word_counts = Counter(words)sorted_word_counts = sorted(word_counts.items(), key=lambda item: item[1], reverse=True)print(sorted_word_counts)# 輸出:[('the', 2), ('quick', 1), ('brown', 1), ('fox', 1), ('jumps', 1), ('over', 1), ('lazy', 1), ('dog', 1)]

結(jié)語

通過以上示例,我們不難發(fā)現(xiàn)sorted()函數(shù)的靈活性和強大功能,它不僅能夠滿足基礎(chǔ)的排序需求,還能通過key和reverse參數(shù)實現(xiàn)復雜的排序邏輯,極大地增強了Python在數(shù)據(jù)處理方面的表現(xiàn)力。無論是在數(shù)據(jù)分析、文本處理還是日常編程中,熟練掌握并運用sorted()函數(shù)都將使你的代碼更加高效、優(yōu)雅。希望本文能激發(fā)你對sorted()函數(shù)更深層次的探索和實踐,讓它成為你編程生涯中不可或缺的得力助手。ZJg28資訊網(wǎng)——每日最新資訊28at.com


ZJg28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-95548-0.html深入探索Python排序神器:sorted()函數(shù)全解析

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: Python十個常用的自動化腳本

下一篇: 使用 Clean Architecture 生成 .NET 項目指南

標簽:
  • 熱門焦點
Top