龙空技术网

一文讲清Python 中所有数据结构的排序

自由坦荡的湖泊AI 146

前言:

而今姐妹们对“python数组排序sort”大概比较着重,咱们都需要学习一些“python数组排序sort”的相关文章。那么小编同时在网上收集了一些关于“python数组排序sort””的相关知识,希望朋友们能喜欢,各位老铁们一起来了解一下吧!

·

在列表中排序使用sorted()函数进行列表排序

使用内置的sorted()函数对列表进行排序提供了一种便捷的方法来获取列表的排序版本,而无需修改原始列表。Sorted() 函数根据元素的自然顺序或自定义排序键返回一个新的排序列表。 示例代码:

Numbers = [ 4 , 2 , 1 , 3 , 5 ] Sorted_numbers = Sorted (numbers) print (sorted_numbers)   # 输出:[1, 2, 3, 4, 5]
使用 sort() 方法进行就地排序

sort() 方法允许对列表进行就地排序,修改原始列表。默认情况下,它按升序排列列表的元素,但可以使用 key 参数进行自定义。示例代码:

Fruits = [ 'apple' , 'banana' , 'cherry' , 'date' ] fruits.sort() print (fruits)   # 输出:['apple', 'banana', 'cherry', 'date']
具有关键功能的自定义排序

Python 的排序函数支持 key 参数,它允许定义一个函数来提取一个值进行比较。这使得可以根据特定标准进行排序,例如根据每个字符串的长度对字符串列表进行排序。示例代码:

Words = [ 'cat' , 'apple' , 'zebra' , 'banana' ] Sorted_words = Sorted (words, key= len )   # 根据字符串长度排序print (sorted_words)   # 输出:['cat', 'apple' 、'zebra' , 'banana'”]
对对象列表进行排序

对对象列表进行排序时,key 参数可用于指定要用于比较的对象的属性或属性。这使得可以根据特定对象属性进行排序。示例代码:

class  Person :     def  __init__ ( self, name,age ):         self.name = name         self.age = 年龄people = [Person( 'John' , 25 ), Person( 'Jane' , 30 ), Person( 'Alice' , 20 )] sorted_people= sorted (people,key= lambdax :x.age) forpersoninsorted_people : print (person.name)   #输出:Alice、John、Jane    
元组排序对元组进行排序:利用其不可变的性质

元组在 Python 中是不可变的,这意味着它们的元素不能被修改。但是,可以通过使用sorted()函数创建排序版本来对元组进行排序,类似于排序列表。示例代码:

tuple_numbers = ( 4 , 2 , 1 , 3 , 5 ) Sorted_tuple = tuple (已排序(tuple_numbers)) print (sorted_tuple)   # 输出:(1, 2, 3, 4, 5)
根据特定标准对元组进行排序

要根据特定条件对元组进行排序,可以将 key 参数与排序()一起使用来定义自定义函数或 lambda 表达式,以提取所需的值进行比较。示例代码:

tuple_words = ( 'cat' , 'apple' , 'zebra' , 'banana' ) Sorted_tuple_words = tuple ( sorted (tuple_words, key= len )) print (sorted_tuple_words)   # 输出: ('cat', 'apple', 'banana '、'斑马')
集合排序集合:唯一性和有序元素

Python 中的集合是唯一元素的无序集合。虽然集合没有特定的顺序,但可以通过将它们转换为列表并使用排序()函数来对它们进行排序。示例代码:

set_numbers = { 4 , 2 , 1 , 3 , 5 } Sorted_set = Sorted (set_numbers) print (sorted_set)   # 输出:[1, 2, 3, 4, 5]
使用sorted()函数对集合进行排序

由于集合是无序的,因此使用sorted()函数对它们进行排序会返回一个基于元素自然顺序的新排序列表。示例代码:

set_words = { 'cat' , 'apple' , 'zebra' , 'banana' } Sorted_set_words = Sorted (set_words) print (sorted_set_words)   # 输出:['apple', 'banana', 'cat', 'zebra']
字典排序按键对字典排序

Python 中的字典是键值对的无序集合。但是,如果您要按排序顺序检索键,则可以将sorted() 函数与字典的keys() 方法结合使用。示例代码:

Dictionary = { 'b' : 2 , 'a' : 1 , 'd' : 4 , 'c' : 3 } Sorted_keys = Sorted (dictionary.keys()) for key in Sorted_keys:     print (key,dictionary[key] )   # 输出:a 1, b 2, c 3, d 4
按值对字典排序

要根据字典的值对字典进行排序,sorted() 函数可以与字典的 items() 方法和自定义键函数或 lambda 表达式结合使用。示例代码:

Dictionary = { 'b' : 2 , 'a' : 1 , 'd' : 4 , 'c' : 3 } Sorted_items = Sorted (dictionary.items(), key= lambda x: x[ 1 ]) for key, value in Sorted_items:     print (key, value)   # 输出:a 1, b 2, c 3, d 4
数组排序理解 Python 中的数组

Python 中的数组是由array模块提供的,允许高效存储同类数据类型。可以使用sorted()函数或数组的sort()方法来对数组进行排序。示例代码:

import array Numbers = array.array( 'i' , [ 4 , 2 , 1 , 3 , 5 ]) Sorted_numbers = Sorted (numbers) print (sorted_numbers)   # 输出:[1, 2, 3, 4, 5]
使用内置函数和方法对数组进行排序

数组提供了一种sort()方法,允许对数组进行就地排序,修改原始数组。该方法默认按升序对元素进行排序。示例代码:

import array Numbers = array.array( 'i' , [ 4 , 2 , 1 , 3 , 5 ]) Numbers.sort() print (numbers)   # 输出: array('i', [1, 2, 3, 4 , 5])
Pandas 排序按列值排序

要按一列或多列对数据框进行排序,可以使用该sort_values()方法。此方法允许您指定要排序的列以及所需的排序顺序(升序或降序)。示例代码:

import pandas as pd # 创建一个示例 DataFrame data = { 'Name' : [ 'Alice' , 'Bob' , 'Charlie' ],         'Age' : [ 25 , 30 , 22 ],         'Salary' : [ 50000 , 60000 , 45000 ]} df = pd.DataFrame(data) # 按 'Salary' 列降序对 DataFrame 进行排序Sorted_df = df.sort_values( 'Salary' , ascending= False ) print (sorted_df)
按多列排序

要按多列对 DataFrame 进行排序,您可以将列名称列表传递给该sort_values()方法。DataFrame 将根据指定的列顺序进行排序。示例代码:

# 按“年龄”升序对 DataFrame 进行排序,然后按“薪水”降序排序Sorted_df = df.sort_values([ 'Age' , 'Salary' ], ascending=[ True , False ]) print (sorted_df)
分选系列

对Series进行排序:要对Series对象进行排序,可以使用 方法sort_values(),类似于对DataFrame进行排序。此方法返回一个带有排序值的新系列。示例代码:

# 创建一个示例 Series s = pd.Series([ 5 , 2 , 7 , 1 , 4 ]) # 按升序对 Series 进行排序排序_s = s.sort_values() print (sorted_s)
按索引排序

如果想根据 Series 的索引对 Series 进行排序,可以使用该sort_index()方法。此方法返回一个新的 Series,其中的值根据索引排序。示例代码:

# 按索引对系列进行排序sorted_s = s.sort_index() print (sorted_s)

标签: #python数组排序sort #数据结构 排序 总结报告