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

news/2024/7/19 12:00:44 标签: 列表, python, java, ajax, php

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 示例


http://www.niftyadmin.cn/n/1254133.html

相关文章

php里的switch,PHP中Switch语句的运用

PHP中Switch语句的运用引导语&#xff1a;PHP 是一种 HTML 内嵌式的语言&#xff0c;是一种在服务器端执行的嵌入HTML文档的脚本语言&#xff0c;语言的风格有类似于C语言&#xff0c;被广泛地运用。以下是小编整理的PHP中Switch语句的运用&#xff0c;欢迎参考阅读!switch 语句…

stack.empty_Java Stack empty()方法与示例

stack.empty堆栈类empty()方法 (Stack Class empty() method) empty() method is available in java.util package. empty()方法在java.util包中可用。 empty() method is used to check whether this stack is empty or not empty. empty()方法用于检查此堆栈是否为空。 empty…

java 跳转app,Android APP打开另一个APP的几种实现总结

需求分析&#xff1a;1.A点击拉起B&#xff1b;2.如果B没安装&#xff0c;下载安装&#xff1b;3.如果B已安转&#xff0c;未在后台运行点击打开B&#xff0c;传值账号密码&#xff0c;做跨登录&#xff1b;4.如果B已安装&#xff0c;且正在后台运行&#xff0c;A打开B直接显示…

hashset add方法_Java HashSet add()方法与示例

hashset add方法HashSet类的add()方法 (HashSet Class add() method) add() method is available in java.util package. add()方法在java.util包中可用。 add() method is used to insert the given element in this HashSet when not already exists otherwise it ignores it…

枚举 equals_Java枚举equals()方法与示例

枚举 equals枚举类equals()方法 (Enum Class equals() method) equals() method is available in java.lang package. equals()方法在java.lang包中可用。 equals() method is used to check equality or inequality of this Object against the given Object or in other word…

在matlab中的平方,在传奇中使用平方而不是Matlab

我有以下代码,它使用imagesc绘制一个’map’,并提供一个图例,参见附加的输出.我试图用实心方块替换图例中的线条.我向远处留下线条和空心方块(包括图中左上角的随机方块)figure(6)imagesc(lut)title(Ditribution of Land use Types)ylabel(Longitude)xlabel(Latitude)caxis([0,…

matlab 阈值变换,求助一个小波变换阈值设定的程序

soundwavread(000);soundsound;subplot(4,1,1);plot(sound);title(原始信号);grid;set(gca,ytick,-1:1:1)count1length(sound);noise0.07*randn(1,count1);%count128000for i1:count1signal(i)sound(i)noise(i);end;for i1:count1y(i)signal(i);end;subplot(4,1,2); plot(y);ti…

date的before方法_Java Date before()方法与示例

date的before方法日期类before()方法 (Date Class before() method) before() method is available in java.util package. before()方法在java.util包中可用。 before() method is used to check whether this date is before the given date (d) or not. before()方法用于检查…