1.使用sort()方法对列表进行永久性排序:

colorsList=['hong','cheng','huang','lv']

colorsList.sort() #正序排序
print(colorsList) colorsList.sort(reverse=True) #倒序 关键字:reverse=True
print(colorsList)

执行结果:

['cheng', 'hong', 'huang', 'lv']
['lv', 'huang', 'hong', 'cheng']

2.使用函数sorted()对列表进行临时排序:(sorted()让你能够按特定顺序显示列表元素,同时不影响它们在列表中的原始排列顺序。)

print('原列表:')
print(colorsList) print('调用sorted()方法升序后的临时列表:')
print(sorted(colorsList)) #升序 print('调用sorted()方法后的原列表:')
print(colorsList) print('原列表:')
print(colorsList) print('调用sorted()方法倒序后的临时列表:')
print(sorted(colorsList,reverse=True)) #倒序 print('调用sorted()方法后的原列表:')
print(colorsList)

执行结果:

原列表:
['hong', 'cheng', 'huang', 'lv']
调用sorted()方法升序后的临时列表:
['cheng', 'hong', 'huang', 'lv']
调用sorted()方法后的原列表:
['hong', 'cheng', 'huang', 'lv']
原列表:
['hong', 'cheng', 'huang', 'lv']
调用sorted()方法倒序后的临时列表:
['lv', 'huang', 'hong', 'cheng']
调用sorted()方法后的原列表:
['hong', 'cheng', 'huang', 'lv']

3.reverse()方法:反转列表元素的排列顺序

colorsList=['hong','cheng','huang','lv']

print('原列表:')
print(colorsList) colorsList.reverse()
print('调用reverse()方法第一次反转后的列表:')
print(colorsList) #第一次反转 colorsList.reverse()
print('调用reverse()方法第二次反转后的列表:')
print(colorsList) #第二次反转

执行结果:

原列表:
['hong', 'cheng', 'huang', 'lv']
调用reverse()方法第一次反转后的列表:
['lv', 'huang', 'cheng', 'hong']
调用reverse()方法第二次反转后的列表:
['hong', 'cheng', 'huang', 'lv']

Python_列表操作2的更多相关文章

  1. Python_列表操作1

    1.列表相关操作:声明,添加,删除,修改,获取len colors=['红','橙','黄','绿'] #声明一个列表 def colors_getall(): #获取列表中所有元素 return c ...

  2. react实例之todo,做一个实时响应的列表操作

    react实例之todo, 做一个实时响应的列表操作 在所有的mvc框架中,最常见的例子不是hello world,而是todo,由于reactjs的简单性,在不引用flux和redux的情况下,我们 ...

  3. python_列表

    1.  列表的定义 (1) 用[]包含的元素,就是列表 (2)可以用list函数定义列表 2.  基本的列表操作(可参考示例) (1)元素赋值 (2)删除元素 (3)分片赋值 3. 列表方法 (1) ...

  4. TCL语言笔记:TCL中的列表操作

    一.介绍 列表则是具有特殊解释的字符串.Tcl 中的列表操作和其它 Tcl 命令一样具有相同的结构.列表可应用在诸如 foreach 这样的以列表为变元的循环命令中,也应于构建 eval 命令的延迟命 ...

  5. Python 基础篇:字符串、列表操作

    字符串操作 判断是否为数字 string = "200" string.isdigit() >>false 待完善.. 列表操作 列表是我们最以后最常用的数据类型之一, ...

  6. 征服 Redis + Jedis + Spring (三)—— 列表操作【转】

    一开始以为Spring下操作哈希表,列表,真就是那么土.恍惚间发现“stringRedisTemplate.opsForList()”的强大,抓紧时间恶补下. 相关链接: 征服 Redis 征服 Re ...

  7. python学习笔记——列表操作

    python列表操作——增 append:追加一条数据到列表的最后 name = ["Zhangsan","XiongDa","Lisi"] ...

  8. Python中的列表操作

    Python的列表操作可谓是功能强大且方便(相对于Java)简单.常规的操作就不说了(这不是一个入门教程),介绍几个很有特点的例子 添加 # 追加到结尾(append) li = [1, 2, 3, ...

  9. [Python Study Notes]列表操作

    列表操作 a.切片 >>> names = ["Alex","Tenglan","Eric","Rain&quo ...

随机推荐

  1. what's the RTP协议

    what's the RTP RTP全名是Real-time Transport Protocol(实时传输协议).它是IETF提出的一个标准,对应的RFC文档为RFC3550(RFC1889为其过期 ...

  2. 根据 sitemap 的规则[0],当前页面 [pages/index/index] 将被索引

    sitemap 的索引提示是默认开启的,如需要关闭 sitemap 的索引提示,可在小程序项目配置文件 project.config.json 的 setting 中配置字段 checkSiteMap ...

  3. dotfuscator 在混淆.Net Framework 4.0以上版本的时候报错的解决方法

    dotfuscator 在混淆.Net Framework 4.0以上版本的时候报错的解决方法 在混淆的时候报错了,错误描述大致如下: Could not find a compatible vers ...

  4. [LeetCode] 216. Combination Sum III 组合之和 III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  5. [LeetCode] 244. Shortest Word Distance II 最短单词距离 II

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

  6. [LeetCode] 543. Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  7. Spring boot后台搭建二集成Shiro添加Remember Me

    上一片文章实现了用户验证  查看 当用户成功登录后,关闭浏览器,重新打开浏览器访问http://localhost:8080,页面会跳转到登录页,因为浏览器的关闭后之前的登录已失效 Shiro提供了R ...

  8. 算法练习之x的平方根,爬楼梯,删除排序链表中的重复元素, 合并两个有序数组

    1.x的平方根 java (1)直接使用函数 class Solution { public int mySqrt(int x) { int rs = 0; rs = (int)Math.sqrt(x ...

  9. layui父页面获取子页面数据

    var doc = layero.find('iframe')[0].contentDocument; var test= $(doc).find("input[name='test']&q ...

  10. Linux系统下Redis单机版的安装详细教程

    Linux系统下Redis单机版的安装详细教程 1.下载软件安装包并上传到root目录 这里以旧版本的3.0进行安装,比较成熟稳定,具体软件可以通过qq群534073451文件下载