python 示例_带有示例的Python列表sort()方法

python 示例

列表sort()方法 (List sort() Method)

sort() method is used to sort the list elements in the ascending and descending order, the method is called with this list (whose elements to be sorted) and accept some optional parameters (explained below under parameters), the method does not return any value, it sorts this list.

sort()方法用于按升序和降序对列表元素进行排序,该方法以该列表(要排序的元素)进行调用,并接受一些可选参数(下面在参数下进行解释),该方法不返回任何参数值,它将对该列表进行排序。

Syntax:

句法:

    List_name.sort(reverse=True|False, key=function)

Parameter(s):

参数:

  • reverse=True|False – It is an optional parameter, it's default value is False which sorts list elements in ascending order, if we set True, it sorts the list elements in descending order.

    reverse = True | False –这是一个可选参数,默认值为False ,它以升序对列表元素进行排序;如果设置True ,则它以降序对列表元素进行排序。

  • key=function – It is also an optional parameter, it can be used to specify the sorting criteria.

    key = function –也是可选参数,可用于指定排序条件。

Return value:

返回值:

The return type of this method is <class 'NoneType'>, it returns nothing.

此方法的返回类型为<class'NoneType'> ,它什么也不返回。

Example 1: Sorting list without specifying any parameter

示例1:不指定任何参数的排序列表

# Python List sort() Method with Example

# declaring the list
cars = ["BMW", "Porsche", "Audi", "Lexus", "Audi"]

# printing the list
print("cars before sort operation...")
print("cars: ", cars)

# sorting the elements
cars.sort() # sorts in ascending order

# printing the list
print("cars after sort operation...")
print("cars: ", cars)

Output

输出量

cars before sort operation...
cars:  ['BMW', 'Porsche', 'Audi', 'Lexus', 'Audi']
cars after sort operation...
cars:  ['Audi', 'Audi', 'BMW', 'Lexus', 'Porsche']

Example 2: Sorting list with specifying the first parameter

示例2:指定第一个参数的排序列表

# Python List sort() Method with Example

# declaring the list
cars = ["BMW", "Porsche", "Audi", "Lexus", "Audi"]

# printing the list
print("cars before sort operation...")
print("cars: ", cars)

# sorting the elements specifying reverse=True
cars.sort(reverse=True) 

# printing the list
print("cars list elements in descending order...")
print("cars: ", cars)

# sorting the elements specifying reverse=False
cars.sort(reverse=False) 

# printing the list
print("cars list elements in ascending order...")
print("cars: ", cars)

Output

输出量

cars before sort operation...
cars:  ['BMW', 'Porsche', 'Audi', 'Lexus', 'Audi']
cars list elements in descending order...
cars:  ['Porsche', 'Lexus', 'BMW', 'Audi', 'Audi']
cars list elements in ascending order...
cars:  ['Audi', 'Audi', 'BMW', 'Lexus', 'Porsche']

Example 3: Sorting list with specifying the both parameters

示例3:同时指定两个参数的排序列表

# Python List sort() Method with Example

# defining a function that will return length
def getLen(e):
    return len(e)
    
# declaring the list
cars = ["BMW", "Porsche", "Audi", "Lexus", "Audi"]

# printing the list
print("cars before sort operation...")
print("cars: ", cars)

# sorting the elements specifying reverse=True
cars.sort(reverse=True,key=getLen) 

# printing the list
print("cars list elements in descending order based on length...")
print("cars: ", cars)

# sorting the elements specifying reverse=False
cars.sort(reverse=False,key=getLen) 

# printing the list
print("cars list elements in ascending order based on length...")
print("cars: ", cars)

Output

输出量

cars before sort operation...
cars:  ['BMW', 'Porsche', 'Audi', 'Lexus', 'Audi']
cars list elements in descending order based on length...
cars:  ['Porsche', 'Lexus', 'Audi', 'Audi', 'BMW']
cars list elements in ascending order based on length...
cars:  ['BMW', 'Audi', 'Audi', 'Lexus', 'Porsche']


翻译自: https://www.includehelp.com/python/list-sort-method-with-example.aspx

python 示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值