最近在学习 python 语言。大致学习了 python 的基础语法。觉得 python 在数据处理中的地位和它的 list 操作密不可分。

特学习了相关的基础操作并在这里做下笔记。
''' Python --version Python 2.7.11 Quote : https://docs.python.org/2/tutorial/datastructures.html#more-on-lists Add by camel97 2017-04 ''' list.append(x) #在列表的末端添加一个新的元素 Add an item to the end of the list; equivalent to a[len(a):] = [x].
list.extend(L)#将两个 list 中的元素合并到一起
Extend the list by appending all the items in the given list; equivalent to a[len(a):] = L.
list.insert(i, x)#将元素插入到指定的位置(位置为索引为 i 的元素的前面一个)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x).
list.remove(x)#删除 list 中第一个值为 x 的元素(即如果 list 中有两个 x , 只会删除第一个 x )
Remove the first item from the list whose value is x. It is an error if there is no such item.
list.pop([i])#删除 list 中的第 i 个元素并且返回这个元素。如果不给参数 i ,将默认删除 list 中最后一个元素
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.)
list.index(x)#返回 list 中 , 值为 X 的元素的索引
Return the index in the list of the first item whose value is x. It is an error if there is no such item.
list.count(x)#返回 list 中 , 值为 x 的元素的个数
Return the number of times x appears in the list.
demo:
#-*-coding:utf-8-*- L = [1,2,3] #创建 list L2 = [4,5,6] print L L.append(6) #添加 print L L.extend(L2) #合并 print L L.insert(0,0) #插入 print L L.remove(6) #删除 print L L.pop() #删除 print L print L.index(2)#索引 print L.count(2)#计数 L.reverse() #倒序 print L
result:
[1, 2, 3] [1, 2, 3, 6] [1, 2, 3, 6, 4, 5, 6] [0, 1, 2, 3, 6, 4, 5, 6] [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5] 2 1 [5, 4, 3, 2, 1, 0]
list.sort(cmp=None, key=None, reverse=False)
Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).
1.对一个 list 进行排序。默认按照从小到大的顺序排序
L = [2,5,3,7,1] L.sort() print L ==>[1, 2, 3, 5, 7] L = ['a','j','g','b'] L.sort() print L ==>['a', 'b', 'g', 'j']
2.reverse 是一个 bool 值. 默认为 False , 如果把它设置为 True, 那么这个 list 中的元素将会被按照相反的比较结果(倒序)排列.
# reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.
L = [2,5,3,7,1] L.sort(reverse = True) print L ==>[7, 5, 3, 2, 1] L = ['a','j','g','b'] L.sort(reverse = True) print L ==>['j', 'g', 'b', 'a']
3.key 是一个函数 , 它指定了排序的关键字 , 通常是一个 lambda 表达式 或者 是一个指定的函数
#key specifies a function of one argument that is used to extract a comparison key from each list element: key=str.lower. The default value is None (compare the elements directly).
#-*-coding:utf-8-*-
#创建一个包含 tuple 的 list 其中tuple 中的三个元素代表名字 , 身高 , 年龄
students = [('John', 170, 15), ('Tom', 160, 12), ('Dave', 180, 10)]
print students
==>[('John', 170, 15), ('Tom', 160, 12), ('Dave', 180, 10)]
students.sort(key = lambda student:student[0])
print students
==>[('Dave', 180, 10), ('John', 170, 15), ('Tom', 160, 12)]#按名字(首字母)排序
students.sort(key = lambda student:student[1])
print students
==>[('Tom', 160, 12), ('John', 170, 15), ('Dave', 180, 10)]#按身高排序
students.sort(key = lambda student:student[2])
print students
==>[('Dave', 180, 10), ('Tom', 160, 12), ('John', 170, 15)]#按年龄排序
4.cmp 是一个指定了两个参数的函数。它决定了排序的方法。
#cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first #argument is considered smaller than, equal to, or larger than the second argument: cmp=lambda x,y: cmp(x.lower(), y.lower()). The default value is None.
#-*-coding:utf-8-*-
students = [('John', 170, 15), ('Tom', 160, 12), ('Dave', 180, 10)]
print students
==>[('John', 170, 15), ('Tom', 160, 12), ('Dave', 180, 10)]
#指定 用第一个字母的大写(ascii码)和第二个字母的小写(ascii码)比较
students.sort(cmp=lambda x,y: cmp(x.upper(), y.lower()),key = lambda student:student[0])
print students
==>[('Dave', 180, 10), ('Tom', 160, 12), ('John', 170, 15)]
#指定 比较两个字母的小写的 ascii 码值
students.sort(cmp=lambda x,y: cmp(x.lower(), y.lower()),key = lambda student:student[0])
print students
==>[('Dave', 180, 10), ('John', 170, 15), ('Tom', 160, 12)]
#cmp(x,y) 是python内建立函数,用于比较2个对象,如果 x < y 返回 -1, 如果 x == y 返回 0, 如果 x > y 返回 1
cmp 可以让用户自定义大小关系。平时我们认为 1 < 2 , 认为 a < b。
现在我们可以自定义函数,通过自定义大小关系(例如 2 < a < 1 < b) 来对 list 进行指定规则的排序。
当我们在处理某些特殊问题时,这往往很有用。
以上所述是小编给大家介绍的Python 中 list 的各项操作技巧,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
# python
# list
# 操作
# Python 列表(List)操作方法详解
# Python中给List添加元素的4种方法分享
# python中list常用操作实例详解
# Python列表(list)常用操作方法小结
# Python实现两个list对应元素相减操作示例
# python对list中的每个元素进行某种操作的方法
# 是一个
# 第一个
# 自定义
# 值为
# 小编
# 将会
# 在此
# 中有
# 并在
# 把它
# 我们可以
# 只会
# 第二个
# 给大家
# 密不可分
# 数据处理
# 不给
# 当我们
# 设置为
# 所述
相关文章:
网站视频怎么制作,哪个网站可以免费收看好莱坞经典大片?
如何在宝塔面板中创建新站点?
胶州企业网站制作公司,青岛石头网络科技有限公司怎么样?
深圳网站制作费用多少钱,读秀,深圳文献港这样的网站很多只提供网上试读,但有些人只要提供试读的文章就能全篇下载,这个是怎么弄的?
建站OpenVZ教程与优化策略:配置指南与性能提升
在线流程图制作网站手机版,谁能推荐几个好的CG原画资源网站么?
如何在万网主机上快速搭建网站?
建站之星如何配置系统实现高效建站?
企业宣传片制作网站有哪些,传媒公司怎么找企业宣传片项目?
宠物网站制作html代码,有没有专门介绍宠物如何养的网站啊?
Swift开发中switch语句值绑定模式
,石家庄四十八中学官网?
美食网站链接制作教程视频,哪个教做美食的网站比较专业点?
如何选择高效便捷的WAP商城建站系统?
做企业网站制作流程,企业网站制作基本流程有哪些?
北京制作网站的公司,北京铁路集团官方网站?
中山网站制作网页,中山新生登记系统登记流程?
魔毅自助建站系统:模板定制与SEO优化一键生成指南
大连网站制作费用,大连新青年网站,五年四班里的视频怎样下载啊?
宝塔新建站点为何无法访问?如何排查?
定制建站方案优化指南:企业官网开发与建站费用解析
如何在阿里云服务器自主搭建网站?
如何快速配置高效服务器建站软件?
如何通过远程VPS快速搭建个人网站?
网站制作企业,网站的banner和导航栏是指什么?
北京网站制作费用多少,建立一个公司网站的费用.有哪些部分,分别要多少钱?
网站制作的软件有哪些,制作微信公众号除了秀米还有哪些比较好用的平台?
如何快速生成专业多端适配建站电话?
西安制作网站公司有哪些,西安货运司机用的最多的app或者网站是什么?
建站之星×万网:智能建站系统+自助建站平台一键生成
Python lxml的etree和ElementTree有什么区别
建站之星导航菜单设置与功能模块配置全攻略
关于BootStrap modal 在IOS9中不能弹出的解决方法(IOS 9 bootstrap modal ios 9 noticework)
网站制作免费,什么网站能看正片电影?
网站制作中优化长尾关键字挖掘的技巧,建一个视频网站需要多少钱?
如何制作一个表白网站视频,关于勇敢表白的小标题?
在线ppt制作网站有哪些,请推荐几个好的课件下载的网站?
Swift中switch语句区间和元组模式匹配
焦点电影公司作品,电影焦点结局是什么?
自助网站制作软件,个人如何自助建网站?
c++怎么使用类型萃取type_traits_c++ 模板元编程类型判断【方法】
SAX解析器是什么,它与DOM在处理大型XML文件时有何不同?
青岛网站设计制作公司,查询青岛招聘信息的网站有哪些?
成都网站制作报价公司,成都工业用气开户费用?
网站制作软件免费下载安装,有哪些免费下载的软件网站?
如何快速查询网站的真实建站时间?
建站主机如何选?性能与价格怎样平衡?
合肥做个网站多少钱,合肥本地有没有比较靠谱的交友平台?
SQL查询语句优化的实用方法总结
如何在建站之星网店版论坛获取技术支持?
*请认真填写需求信息,我们会在24小时内与您取得联系。