GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — Python3 sorted() 函数 | 菜鸟教程
Python 3 教程
Python3 教程
Python3 简介
Python3 环境搭建
Python3 Pycharm
Python3 VScode
Python3 基础语法
Python3 基本数据类型
Python3 数据类型转换
Python3 解释器
Python3 注释
Python3 运算符
Python3 数字(Number)
Python3 字符串
Python3 列表
Python3 元组
Python3 字典
Python3 集合
Python3 条件控制
Python3 循环语句
Python3 编程第一步
Python3 推导式
Python3 迭代器与生成器
Python3 with
Python3 函数
Python3 lambda
Python3 装饰器
Python3 数据结构
Python3 模块
Python __name__
Python3 输入和输出
Python3 File
Python3 OS
Python3 错误和异常
Python3 面向对象
Python3 命名空间/作用域
Python 虚拟环境的创建
Python 类型注解
Python3 标准库概览
Python3 实例
Python 测验
Python3 高级教程Python3 正则表达式 Python3 CGI编程 Python3 MySQL(mysql-connector) Python3 MySQL(PyMySQL) Python3 网络编程 Python3 SMTP发送邮件 Python3 多线程 Python3 XML 解析 Python3 JSON Python3 日期和时间 Python3 内置函数 Python3 MongoDB Python3 urllib Python uWSGI 安装配置 Python3 pip Python3 operator Python math Python requests Python random Python OpenAI Python 有用的资源 Python AI 绘画 Python statistics Python hashlib Python 量化 Python pyecharts Python selenium 库 Python 爬虫 Python Scrapy 库 Python Markdown Python sys 模块 Python Pickle 模块 Python subprocess 模块 Python queue 模块 Python StringIO 模块 Python logging 模块 Python datetime 模块 Python re 模块 Python csv 模块 Python threading 模块 Python asyncio 模块 Python PyQtPython3 sorted() 函数描述sorted() 函数对所有可迭代的对象进行排序操作。
语法sorted 语法: sorted(iterable, key=None, reverse=False) 参数说明:
返回值返回重新排序的列表。 实例以下实例展示了 sorted 的最简单的使用方法:
>>> sorted([5, 2, 3, 1, 4]) [1, 2, 3, 4, 5] # 默认为升序 你也可以使用 list 的 list.sort() 方法。这个方法会修改原始的 list(返回值为None)。通常这个方法不如sorted()方便-如果你不需要原始的 list,list.sort()方法效率会稍微高一些。
>>> a=[5,2,3,1,4] >>> a.sort() >>> a [1,2,3,4,5] 另一个区别在于list.sort() 方法只为 list 定义。而 sorted() 函数可以接收任何的 iterable。
>>> sorted({1: 'D', 2: 'B', 3: 'B', 4: 'E', 5: 'A'}) [1, 2, 3, 4, 5] 利用key进行倒序排序
>>> example_list = [5, 0, 6, 1, 2, 7, 3, 4] >>> result_list = sorted(example_list, key=lambda x: x*-1) >>> print(result_list) [7, 6, 5, 4, 3, 2, 1, 0] >>> 要进行反向排序,也通过传入第三个参数 reverse=True:
>>> example_list = [5, 0, 6, 1, 2, 7, 3, 4] >>> sorted(example_list, reverse=True) [7, 6, 5, 4, 3, 2, 1, 0] 以下是个更复杂的实例,对奖牌榜进行排序,数据里面包含了15个国家的金、银、铜数据,使用 \n 作为分隔符: 实例
s = "德国 10 11 16\n意大利 10 10 20\n荷兰 10 12 14\n法国 10 12 11\n英国 22 21 22\n中国 38 32 18\n日本 27 14 17\n美国 39 41 33\n俄罗斯奥委会 20 28 23\n澳大利亚 17 7 22\n匈牙利 6 7 7\n加拿大 7 6 11\n古巴 7 3 5\n巴西 7 6 8\n新西兰 7 6 7" stodata = s.split('\n',-1) # 使用sorted para = {} for line in range(len(stodata)): # 每一行数据 data = stodata[line].split(' ') print(data) # 组装数据结构para={'China': [], 'Russia': []} para[data[0]] = [int('-' + i) for i in data[1:]] # 开始排序(x[1]代表奖牌数目, x[0]代表国家) new_para = sorted(para.items(), key=lambda x: (x[1], x[0])) print() c=[] for i in new_para: c.append((i[0])) for j in range(15): print(f"{(j+1):2d} {c[j]}") 输出结果为: ['德国', '10', '11', '16'] ['意大利', '10', '10', '20'] ['荷兰', '10', '12', '14'] ['法国', '10', '12', '11'] ['英国', '22', '21', '22'] ['中国', '38', '32', '18'] ['日本', '27', '14', '17'] ['美国', '39', '41', '33'] ['俄罗斯奥委会', '20', '28', '23'] ['澳大利亚', '17', '7', '22'] ['匈牙利', '6', '7', '7'] ['加拿大', '7', '6', '11'] ['古巴', '7', '3', '5'] ['巴西', '7', '6', '8'] ['新西兰', '7', '6', '7'] 1 美国 2 中国 3 日本 4 英国 5 俄罗斯奥委会 6 澳大利亚 7 荷兰 8 法国 9 德国 10 意大利 11 加拿大 12 巴西 13 新西兰 14 古巴 15 匈牙利
AI 思考中...
|
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |