python3多重排序處理多數(shù)據(jù)的示例詳解
前言
主要講解多種方式的處理,以實(shí)際的Demo為主
| 方法 | 優(yōu)點(diǎn) | 缺點(diǎn) |
|---|---|---|
| 內(nèi)置 sorted() 函數(shù)與 lambda 表達(dá)式 | 簡(jiǎn)單易用,代碼簡(jiǎn)潔 適合處理較小的數(shù)據(jù)集 | 對(duì)于大型數(shù)據(jù)集,性能可能不如專用的庫(kù)高效 |
| operator 模塊 | 提高可讀性,尤其是在復(fù)雜鍵提取的情況下 | 仍然是基于 sorted() 的實(shí)現(xiàn),對(duì)于大型數(shù)據(jù)集,性能有限 |
| pandas 庫(kù) | 高效處理大型數(shù)據(jù)集 提供豐富的數(shù)據(jù)操作功能 | 需要學(xué)習(xí)和掌握 pandas 庫(kù)的使用 |
| numpy 庫(kù) | 高效處理數(shù)值數(shù)據(jù) 適用于大型數(shù)值數(shù)據(jù)集 | 對(duì)于非數(shù)值數(shù)據(jù)(如字符串),可能不如 pandas 方便 |
1. 內(nèi)置 sorted() 函數(shù)與 lambda 表達(dá)式
提供一個(gè)鍵函數(shù)來(lái)實(shí)現(xiàn)多重排序
鍵函數(shù)可以是一個(gè) lambda 表達(dá)式,用來(lái)返回一個(gè)元組,元組中的每個(gè)元素按照優(yōu)先級(jí)進(jìn)行排序
data = [
{'name': 'Alice', 'age': 30, 'score': 88},
{'name': 'Bob', 'age': 25, 'score': 92},
{'name': 'Charlie', 'age': 30, 'score': 95},
{'name': 'David', 'age': 25, 'score': 85}
]
# 按 age 和 score 排序,age 升序,score 降序
sorted_data = sorted(data, key=lambda x: (x['age'], -x['score']))
print(sorted_data)
截圖大致如下:

sorted() 函數(shù)不僅可以處理數(shù)字?jǐn)?shù)據(jù),還可以處理字符串和其他非數(shù)字?jǐn)?shù)據(jù)
通過(guò) lambda 表達(dá)式,可以指定任意的排序邏輯
data = [
{'name': 'Alice', 'department': 'HR', 'role': 'Manager'},
{'name': 'Bob', 'department': 'Engineering', 'role': 'Developer'},
{'name': 'Charlie', 'department': 'HR', 'role': 'Assistant'},
{'name': 'David', 'department': 'Engineering', 'role': 'Manager'}
]
# 按 department 和 role 排序
sorted_data = sorted(data, key=lambda x: (x['department'], x['role']))
print(sorted_data)
數(shù)據(jù)先按 department 字段排序,再按 role 字段排序
2. operator 模塊
from operator import itemgetter
data = [
{'name': 'Alice', 'age': 30, 'score': 88},
{'name': 'Bob', 'age': 25, 'score': 92},
{'name': 'Charlie', 'age': 30, 'score': 95},
{'name': 'David', 'age': 25, 'score': 85}
]
# 按 age 和 score 排序,age 升序,score 降序
sorted_data = sorted(data, key=itemgetter('age', 'score'), reverse=True)
# 注意:要實(shí)現(xiàn) age 升序,score 降序,我們需要稍作調(diào)整
sorted_data = sorted(sorted_data, key=itemgetter('age'))
print(sorted_data)
operator 模塊中的 itemgetter 函數(shù)也適用于非數(shù)字?jǐn)?shù)據(jù)的排序
from operator import itemgetter
data = [
{'name': 'Alice', 'department': 'HR', 'role': 'Manager'},
{'name': 'Bob', 'department': 'Engineering', 'role': 'Developer'},
{'name': 'Charlie', 'department': 'HR', 'role': 'Assistant'},
{'name': 'David', 'department': 'Engineering', 'role': 'Manager'}
]
# 按 department 和 role 排序
sorted_data = sorted(data, key=itemgetter('department', 'role'))
print(sorted_data)
3. pandas 庫(kù)
大型數(shù)據(jù)集,使用 pandas 庫(kù)可以更高效地進(jìn)行多重排序
import pandas as pd
data = [
{'name': 'Alice', 'age': 30, 'score': 88},
{'name': 'Bob', 'age': 25, 'score': 92},
{'name': 'Charlie', 'age': 30, 'score': 95},
{'name': 'David', 'age': 25, 'score': 85}
]
df = pd.DataFrame(data)
# 按 age 升序和 score 降序排序
sorted_df = df.sort_values(by=['age', 'score'], ascending=[True, False])
print(sorted_df)
同樣可處理非數(shù)字
import pandas as pd
data = [
{'name': 'Alice', 'department': 'HR', 'role': 'Manager'},
{'name': 'Bob', 'department': 'Engineering', 'role': 'Developer'},
{'name': 'Charlie', 'department': 'HR', 'role': 'Assistant'},
{'name': 'David', 'department': 'Engineering', 'role': 'Manager'}
]
df = pd.DataFrame(data)
# 按 department 和 role 排序
sorted_df = df.sort_values(by=['department', 'role'])
print(sorted_df)
4. numpy 庫(kù)
適用于數(shù)值數(shù)據(jù)
import numpy as np
data = np.array([
('Alice', 30, 88),
('Bob', 25, 92),
('Charlie', 30, 95),
('David', 25, 85)
], dtype=[('name', 'U10'), ('age', 'i4'), ('score', 'i4')])
# 按 age 升序和 score 降序排序
sorted_data = np.sort(data, order=['age', 'score'])[::-1]
sorted_data = np.sort(sorted_data, order=['age'])
print(sorted_data)
5. 自定義
某些情況下,可能需要更復(fù)雜的排序邏輯,可以定義自定義排序函數(shù)并將其傳遞給 sorted() 函數(shù)
data = [
{'name': 'Alice', 'department': 'HR', 'role': 'Manager'},
{'name': 'Bob', 'department': 'Engineering', 'role': 'Developer'},
{'name': 'Charlie', 'department': 'HR', 'role': 'Assistant'},
{'name': 'David', 'department': 'Engineering', 'role': 'Manager'}
]
# 自定義排序函數(shù)
def custom_sort(item):
return (item['department'], item['role'])
# 按 department 和 role 排序
sorted_data = sorted(data, key=custom_sort)
print(sorted_data)
截圖如下:

到此這篇關(guān)于python3多重排序處理多數(shù)據(jù)的示例詳解的文章就介紹到這了,更多相關(guān)python3多重排序處理多數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于python編寫(xiě)的五個(gè)拿來(lái)就能用的炫酷表白代碼分享
七夕快到了,所以本文小編將給給大家介紹五種拿來(lái)就能用的炫酷表白代碼,無(wú)限彈窗表白,愛(ài)心發(fā)射,心動(dòng)表白,玫瑰花等表白代碼,需要的小伙伴快來(lái)試試吧2023-08-08
淺談使用Python變量時(shí)要避免的3個(gè)錯(cuò)誤
這篇文章主要介紹了淺談使用Python變量時(shí)要避免的3個(gè)錯(cuò)誤,還是比較不錯(cuò)的,涉及部分代碼分析,以及字典的創(chuàng)建等相關(guān)內(nèi)容,需要的朋友可以參考下。2017-10-10
matplotlib 生成的圖像中無(wú)法顯示中文字符的解決方法
這篇文章主要介紹了matplotlib 生成的圖像中無(wú)法顯示中文字符的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Python坐標(biāo)軸操作及設(shè)置代碼實(shí)例
這篇文章主要介紹了Python坐標(biāo)軸操作及設(shè)置代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Gradio構(gòu)建交互式Python應(yīng)用使用示例詳解
這篇文章主要為大家介紹了Gradio構(gòu)建交互式Python應(yīng)用使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
Pandas中字符串處理的核心方法與正則匹配實(shí)戰(zhàn)教學(xué)
本文介紹了Pandas中字符串處理的核心方法,重點(diǎn)講解了str訪問(wèn)器的常用操作技巧,包括字符串分割、拼接、替換與提取等,同時(shí)深入講解了正則表達(dá)式的基礎(chǔ)語(yǔ)法及其在Pandas中的應(yīng)用,希望幫助大家掌握高效處理文本數(shù)據(jù)的技能2026-05-05
python爬蟲(chóng)開(kāi)發(fā)之selenium模塊詳細(xì)使用方法與實(shí)例全解
這篇文章主要介紹了python爬蟲(chóng)開(kāi)發(fā)之selenium模塊詳細(xì)使用方法與實(shí)例全解,selenium模塊詳細(xì)在爬蟲(chóng)開(kāi)發(fā)中主要用來(lái)解決JavaScript渲染問(wèn)題需要的朋友可以參考下2020-03-03
Python中Pytest測(cè)試框架的fixture使用詳解
這篇文章主要介紹了Python中Pytest測(cè)試框架的fixture使用詳解,Pytest的fixture的目的是提供一個(gè)測(cè)試的基線,在此基線基礎(chǔ)上,可以更可靠的進(jìn)行重復(fù)測(cè)試,需要的朋友可以參考下2023-08-08

