Python List sort()方法

Python 教程 · 2019-02-02 23:13:13

描述

sort() 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数。

语法

sort()方法语法:

list.sort(cmp=None, key=None, reverse=False)

参数

  • cmp -- 可选参数, 如果指定了该参数会使用该参数的方法进行排序。
  • key -- 主要是用来进行比较的元素,只有一个参数,具体的函数的参数就是取自于可迭代对象中,指定可迭代对象中的一个元素来进行排序。
  • reverse -- 排序规则,reverse = True 降序, reverse = False 升序(默认)。

返回值

该方法没有返回值,但是会对列表的对象进行排序。

实例

以下实例展示了 sort() 函数的使用方法:

实例

#!/usr/bin/python # -*- coding: UTF-8 -*- aList = [123, 'Google', 'Codercto', 'Taobao', 'Facebook']; aList.sort(); print "List : ", aList

以上实例输出结果如下:

List :  [123, 'Facebook', 'Google', 'Codercto', 'Taobao']

以下实例降序输出列表:

实例

#!/usr/bin/python # -*- coding: UTF-8 -*- # 列表 vowels = ['e', 'a', 'u', 'o', 'i'] # 降序 vowels.sort(reverse=True) # 输出结果 print '降序输出:', vowels

以上实例输出结果如下:

降序输出: ['u', 'o', 'i', 'e', 'a']

以下实例演示了通过指定列表中的元素排序来输出列表:

实例

#!/usr/bin/python # -*- coding: UTF-8 -*- # 获取列表的第二个元素 def takeSecond(elem): return elem[1] # 列表 random = [(2, 2), (3, 4), (4, 1), (1, 3)] # 指定第二个元素排序 random.sort(key=takeSecond) # 输出类别 print '排序列表:', random

以上实例输出结果如下:

排序列表:[(4, 1), (2, 2), (1, 3), (3, 4)]

点击查看所有 Python 教程 文章: https://codercto.com/courses/l/8.html

查看所有标签

C# Primer Plus

C# Primer Plus

Klaus Michelsen / Sams / 2001-12-15 / USD 49.99

C# Primer Plus is a tutorial based introduction to the C# language and important parts of the .Net Framework. Throughout the book the reader will be exposed to proven principles enabling him to write ......一起来看看 《C# Primer Plus》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具