Python3学习笔记之列表方法示例详解


Posted in Python onOctober 06, 2017

前言

本文主要给大家介绍了关于Python3列表方法的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

1 使用[]或者list()创建列表

user = []
user = list()

2 使用list() 可以将其他类型转换成列表

# 将字符串转成列表
>>> list('abcde')
['a', 'b', 'c', 'd', 'e']

# 将元祖转成列表
>>> list(('a','b','c'))
['a', 'b', 'c']

3 使用[offset]获取元素 或 修改元素

>>> users = ['a','b','c','d','e']
# 可以使用整数来获取某个元素
>>> users[0]
'a'
# 可以使用负整数来表示从尾部获取某个元素
>>> users[-1]
'e'

# 数组越界会报错
>>> users[100]
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> users[-100]
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: list index out of range

# 修改某个元素
>>> users[0] = 'wdd'
>>> users
['wdd', 'b', 'c', 'd', 'e']
>>>

4 列表切片与提取元素

列表的切片或者提取之后仍然是一个列表

形式如:list[start:end:step]

>>> users
['wdd', 'b', 'c', 'd', 'e']
# 正常截取 注意这里并不会截取到users[2]
>>> users[0:2]
['wdd', 'b']
# 也可从尾部截取
>>> users[0:-2]
['wdd', 'b', 'c']
# 这样可以获取所有的元素
>>> users[:]
['wdd', 'b', 'c', 'd', 'e']
# 也可以加上步长参数
>>> users[0:4:2]
['wdd', 'c']
# 也可以通过这种方式去将列表取反
>>> users[::-1]
['e', 'd', 'c', 'b', 'wdd']

# 注意切片时,偏移量可以越界,越界之后不会报错,仍然按照界限来处理 例如开始偏移量如果小于0,那么仍然会按照0去计算。
>>> users
['wdd', 'b', 'c', 'd', 'e']
>>> users[-100:3]
['wdd', 'b', 'c']
>>> users[-100:100]
['wdd', 'b', 'c', 'd', 'e']
>>>

5 使用append()添加元素至尾部

形式如:list.append(item)

>>> users
['wdd', 'b', 'c', 'd', 'e']
>>> users.append('ddw')
>>> users
['wdd', 'b', 'c', 'd', 'e', 'ddw']

6 使用extend()或+=合并列表

形式如:list1.extend(list2)

这两个方法都会直接修改原列表

>>> users
['wdd', 'b', 'c', 'd', 'e', 'ddw']
>>> names
['heihei', 'haha']
>>> users.extend(names)
>>> users
['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha']
>>> users += names
>>> users
['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha']

7 使用insert()在指定位置插入元素

形式如:list.insert(offset, item)

insert也不存在越界的问题,偏移量正负都行,越界之后会自动伸缩到界限之内,并不会报错

>>> users
['wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha']
>>> users.insert(0,'xiaoxiao')
>>> users
['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', 'haha']
>>> users.insert(-1,'-xiaoxiao')
>>> users
['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha']
# 下面-100肯定越界了
>>> users.insert(-100,'-xiaoxiao')
>>> users
['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha']
# 下面100也是越界了
>>> users.insert(100,'-xiaoxiao')
>>> users
['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']

8 使用del删除某个元素

形式如:del list[offset]

del是python的语句,而不是列表的方法,del删除不存在的元素时,也会提示越界

>>> users
['-xiaoxiao', 'xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
>>> del users[0]
>>> users
['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
>>> del users[100]
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> del users[-100]
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

9 使用remove删除具有指定值的元素

形式如:list.remove(value)

>>> users
['xiaoxiao', 'wdd', 'b', 'c', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
# 删除指定值'c'
>>> users.remove('c')
>>> users
['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'haha', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
# 删除不存在的值会报错
>>> users.remove('alsdkfjalsdf')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
# 如果该值存在多个,那么只能删除到第一个
>>> users.remove('haha')
>>> users
['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']

10 使用pop()方式返回某个元素后,并在数组里删除它

形式如:list.pop(offset=-1) 偏移量默认等于-1,也就是删除最后的元素

>>> users
['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha', '-xiaoxiao']
# 删除最后的元素
>>> users.pop()
'-xiaoxiao'
>>> users
['xiaoxiao', 'wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
# 如果列表本身就是空的,那么pop时会报错
>>> user.pop(0)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: pop from empty list
>>> users.pop(0)
'xiaoxiao'
>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
# 越界时也会报错
>>> users.pop(100)
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
IndexError: pop index out of range

11 使用index()查询具有特定值的元素位置

形式如:list.index(value)

# index只会返回第一遇到该值得位置
>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
>>> users.index('heihei')
5

# 如果该值不存在,也会报错
>>> users.index('laksdf')
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
ValueError: 'laksdf' is not in list

12 使用in判断值是否存在列表

形式如:value in list

>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
>>> 'wdd' in users
True

13 使用count()记录特定值出现的次数

形式如:list.count(value)

>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
>>> users.count('heihei')
2
>>> users.count('h')
0

14 使用join()将列表转为字符串

形式如:string.join(list)

>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
>>> ','.join(users)
'wdd,b,d,e,ddw,heihei,heihei,-xiaoxiao,haha'
>>> user
[]
>>> ','.join(user)
''

15 使用sort()重新排列列表元素

形式如:list.sort()

>>> users
['wdd', 'b', 'd', 'e', 'ddw', 'heihei', 'heihei', '-xiaoxiao', 'haha']
# 默认是升序排序
>>> users.sort()
>>> users
['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']
# 加入reverse=True, 可以降序排序
>>> users.sort(reverse=True)
>>> users
['wdd', 'heihei', 'heihei', 'haha', 'e', 'ddw', 'd', 'b', '-xiaoxiao']

# 通过匿名函数,传入函数进行自定义排序
>>> students
[{'name': 'wdd', 'age': 343}, {'name': 'ddw', 'age': 43}, {'name': 'jik', 'age': 90}]
>>> students.sort(key=lambda item: item['age'])
>>> students
[{'name': 'ddw', 'age': 43}, {'name': 'jik', 'age': 90}, {'name': 'wdd', 'age': 343}]
>>> students.sort(key=lambda item: item['age'], reverse=True)
>>> students
[{'name': 'wdd', 'age': 343}, {'name': 'jik', 'age': 90}, {'name': 'ddw', 'age': 43}]
>>>

16 使用reverse()将列表翻转

形式如:list.reverse()

>>> users
['wdd', 'heihei', 'heihei', 'haha', 'e', 'ddw', 'd', 'b', '-xiaoxiao']
>>> users.reverse()
>>> users
['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']

17 使用copy()复制列表

形式如:list2 = list1.copy()

list2 = list1 这种并不是列表的复制,只是给列表起了别名。实际上还是指向同一个值。

>>> users
['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']
>>> users2 = users.copy()
>>> users2
['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']
>>>

18 使用clear()清空列表

形式如: list.clear()

>>> users2
['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']
>>> users2.clear()
>>> users2
[]

19 使用len()获取列表长度

形式如:len(list)

>>> users
['-xiaoxiao', 'b', 'd', 'ddw', 'e', 'haha', 'heihei', 'heihei', 'wdd']
>>> len(users)
9

20 关于列表越界的深入思考

写了这些方法后,我有一些疑问,为什么有些操作会提示越界,有些则不会呢?

会提示偏移量越界的操作有

  • list[offset] 读取或者修改某个元素
  • del list[offset] 删除指定位置的元素
  • list.remove(value) 删除指定值的元素
  • list.pop(offset) 删除指定位置的元素

如果偏移量越界,这些方法会报错的。我的个人理解是:

假如我想读取偏移量为10的元素,但是该元素并不存在,如果系统自动给你读取了列表的最后一个元素,而且不报错,这是无法容忍的bug。 如果我想删除第10个元素,但是第10个元素并不存在,而系统帮你删除了列表的最后一个元素,我觉得这也是无法容忍的。

所以在使用这些方法时,务必确认该偏移量的元素是否存,否则可能会报错。

总结

以上就是这篇文章的全部内容,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
python抓取百度首页的方法
May 19 Python
Python在不同目录下导入模块的实现方法
Oct 27 Python
wxPython的安装图文教程(Windows)
Dec 28 Python
Python冲顶大会 快来答题!
Jan 17 Python
对python同一个文件夹里面不同.py文件的交叉引用方法详解
Dec 15 Python
python并发编程多进程之守护进程原理解析
Aug 20 Python
python数据处理之如何选取csv文件中某几行的数据
Sep 02 Python
pyqt5 QlistView列表显示的实现示例
Mar 24 Python
Python文件操作及内置函数flush原理解析
Oct 13 Python
Django数据统计功能count()的使用
Nov 30 Python
python 实现数据库中数据添加、查询与更新的示例代码
Dec 07 Python
python pyhs2 的安装操作
Apr 07 Python
python获取外网IP并发邮件的实现方法
Oct 01 #Python
Python之自动获取公网IP的实例讲解
Oct 01 #Python
使用paramiko远程执行命令、下发文件的实例
Oct 01 #Python
解决Scrapy安装错误:Microsoft Visual C++ 14.0 is required...
Oct 01 #Python
win10下Python3.6安装、配置以及pip安装包教程
Oct 01 #Python
Python实现字符串反转的常用方法分析【4种方法】
Sep 30 #Python
Python实现利用最大公约数求三个正整数的最小公倍数示例
Sep 30 #Python
You might like
php 记录进行累加并显示总时长为秒的结果
2011/11/04 PHP
如何在Ubuntu下启动Apache的Rewrite功能
2013/07/05 PHP
yii实现图片上传及缩略图生成的方法
2014/12/04 PHP
php中实现用数组妩媚地生成要执行的sql语句
2015/07/10 PHP
锋利的jQuery 要点归纳(一) jQuery选择器
2010/03/21 Javascript
js/jquery获取浏览器窗口可视区域高度和宽度以及滚动条高度实现代码
2012/12/17 Javascript
解决Extjs 4 Panel作为Window组件的子组件时出现双重边框问题
2013/01/11 Javascript
javascript元素动态创建实现方法
2015/05/13 Javascript
js实现可折叠展开的手风琴菜单效果
2015/09/07 Javascript
js日期插件dateHelp获取本月、三个月、今年的日期
2016/03/07 Javascript
jQuery Easy UI中根据第一个下拉框选中的值设置第二个下拉框是否可以编辑
2016/11/29 Javascript
angularjs+bootstrap菜单的使用示例代码
2017/03/07 Javascript
webpack源码之loader机制详解
2018/04/06 Javascript
JS获取子节点、父节点和兄弟节点的方法实例总结
2018/07/06 Javascript
vue引入axios同源跨域问题
2018/09/27 Javascript
Vue Render函数创建DOM节点代码实例
2020/07/08 Javascript
python获取当前用户的主目录路径方法(推荐)
2017/01/12 Python
wxPython的安装与使用教程
2018/08/31 Python
Selenium定时刷新网页的实现代码
2018/10/31 Python
Python搭建代理IP池实现检测IP的方法
2019/10/27 Python
关于python pycharm中输出的内容不全的解决办法
2020/01/10 Python
python自动化unittest yaml使用过程解析
2020/02/03 Python
python 轮询执行某函数的2种方式
2020/05/03 Python
Python hashlib和hmac模块使用方法解析
2020/12/08 Python
HTML5图片预览实例分享
2014/06/04 HTML / CSS
澳大利亚制造的蜡烛和扩散器:Glasshouse Fragrances
2018/05/20 全球购物
益模软件Java笔试题
2012/03/27 面试题
AJAX应用和传统Web应用有什么不同
2013/08/24 面试题
个人实用的自我评价范文
2013/11/23 职场文书
如何写好升职自荐信
2014/01/06 职场文书
保安拾金不昧表扬信
2014/01/15 职场文书
村级环境卫生整治方案
2014/05/04 职场文书
一个都不能少观后感
2015/06/04 职场文书
国富论读书笔记
2015/06/26 职场文书
2016年度继续教育学习心得体会
2016/01/19 职场文书
漫画「狩龙人拉格纳」公开TV动画预告图
2022/03/22 日漫