作者:韩信子@ShowMeAI
教程地址:http://www.showmeai.tech/tutorials/56
本文地址:http://www.showmeai.tech/article-detail/77
申明:版权所有,转载请分割平台与作者并注明出处
1.Python列表(List)
序列是Python中最根本和常见的数据结构。序列中的每个元素都调配一个数字 - 【它的地位,或索引】,第一个索引是0,第二个索引是1,依此类推。
序列都能够进行的操作包含索引,切片,加,乘,查看成员。
此外,Python曾经内置确定序列的长度以及确定最大和最小的元素的办法。
列表是最罕用的Python数据类型,它能够作为一个方括号内的逗号分隔值呈现。
列表的数据项不须要具备雷同的类型。
创立一个列表,只有把逗号分隔的不同的数据项应用方括号括起来即可。如下所示:
list1 = ['python', 'ShowMeAI', 1997, 2022]list2 = [1, 2, 3, 4, 5 ]list3 = ["a", "b", "c", "d"]
与字符串的索引一样,列表索引从0开始。列表能够进行截取、组合等。
2.拜访列表中的值
应用下标索引来拜访列表中的值,同样你也能够应用方括号的模式截取子列表。
如下为示例代码(代码能够在在线python3环境中运行):
list1 = ['python', 'ShowMeAI', 1997, 2022]list2 = [1, 2, 3, 4, 5, 6, 7 ] print("list1[0]: ", list1[0])print("list2[1:5]: ", list2[1:5])
以上代码执行后果:
list1[0]: pythonlist2[1:5]: [2, 3, 4, 5]
如下为示例代码(代码能够在在线python3环境中运行):
list = ['red', 'green', 'blue', 'yellow', 'white', 'black']print( list[-1] )print( list[:-2] )print( list[-3:] )
运行后果
black['red', 'green', 'blue', 'yellow']['yellow', 'white', 'black']
3.更新列表
你能够对列表的数据项进行批改或更新,你也能够应用append()办法来增加列表项,如下所示(代码能够在在线python3环境中运行):
list = [] ## 空列表list.append('Google') ## 应用 append() 增加元素list.append('ShowMeAI')print(list)
以上代码执行后果:
['Google', 'ShowMeAI']
4.删除列表元素
能够应用 del 语句来删除列表的元素,如下所示(代码能够在在线python3环境中运行):
list1 = ['python', 'ShowMeAI', 1997, 2022]print(list1)del list1[2]print("删除索引为2的元素后 : ")print(list1)
以上代码执行后果:
['python', 'ShowMeAI', 1997, 2022]删除索引为2的元素后 : ['python', 'ShowMeAI', 2022]
留神:咱们后续会探讨remove()办法的应用
5.Python列表脚本操作符
列表对 + 和 的操作符与字符串类似。+ 号用于组合列表, 号用于反复列表。
如下所示:
Python 表达式 | 后果 | 形容 |
---|---|---|
len([1, 2, 3]) | 3 | 长度 |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | 组合 |
['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | 反复 |
3 in [1, 2, 3] | True | 元素是否存在于列表中 |
for x in [1, 2, 3]: print x, | 1 2 3 | 迭代 |
6.Python列表截取
Python 的列表截取实例如下:
>>>L = ['Google', 'ShowMeAI', 'Baidu']>>> L[2]'Baidu'>>> L[-2]'ShowMeAI'>>> L[1:]['ShowMeAI', 'Baidu']>>>
形容:
Python 表达式 | 后果 | 形容 |
---|---|---|
L[2] | 'Baidu' | 读取列表中第三个元素 |
L[-2] | 'ShowMeAI' | 读取列表中倒数第二个元素 |
L[1:] | ['ShowMeAI', 'Baidu'] | 从第二个元素开始截取列表 |
7.Python列表函数&办法
Python蕴含以下函数:
序号 | 函数 | 作用 |
---|---|---|
1 | len(list) | 列表元素个数 |
2 | max(list) | 返回列表元素最大值 |
3 | min(list) | 返回列表元素最小值 |
4 | list(seq) | 将元组转换为列表 |
# 示例1:长度list1, list2 = [123, 'ShowMeAI', 'google'], [456, 'abc']print("第1个列表长度 : ", len(list1))print("第2个列表长度 : ", len(list2))
后果
第1个列表长度 : 3第2个列表长度 : 2
# 示例2:最大最小值list1, list2 = ['Baidu', 'ShowMeAI', 'google'], [456, 789, 200]print("第1个列表最大值 : ", max(list1))print("第1个列表最小值 : ", min(list1))print("第2个列表最大值 : ", max(list2))print("第2个列表最小值 : ", min(list2))
后果
第1个列表最大值 : google第1个列表最小值 : Baidu第2个列表最大值 : 789第2个列表最小值 : 200
# 示例3:转列表aTuple = (123, 'ShowMeAI', 'google', 'Baidu');aList = list(aTuple) print("列表元素 : ")print(aList)
后果
列表元素 : [123, 'ShowMeAI', 'google', 'Baidu']
Python蕴含以下办法:
序号 | 办法 | 作用 |
---|---|---|
1 | list.append(obj) | 在列表开端增加新的对象 |
2 | list.count(obj) | 统计某个元素在列表中呈现的次数 |
3 | list.extend(seq) | 在列表开端一次性追加另一个序列中的多个值(用新列表扩大原来的列表) |
4 | list.index(obj) | 从列表中找出某个值第一个匹配项的索引地位 |
5 | list.insert(index, obj) | 将对象插入列表 |
6 | list.pop([index=-1]) | 移除列表中的一个元素(默认最初一个元素),并且返回该元素的值 |
7 | list.remove(obj) | 移除列表中某个值的第一个匹配项 |
8 | list.reverse() | 反向列表中元素 |
9 | list.sort(cmp=None, key=None, reverse=False) | 对原列表进行排序 |
aList = ['Baidu', 'ShowMeAI', 'google']print("aList : ", aList)aList.append( 'ByteDance' )aList += ['ShowMeAI']print("通过append和+计算后的aList : ", aList)print("统计ShowMeAI个数 : ", aList.count('ShowMeAI'))aList.extend(['Taobao', 'Tencent'])print("通过extend后的aList : ", aList)print("应用index查找Taobao的索引地位: ", aList.index('Taobao'))aList.insert( 3, 'DiDi')print("在index为3的地位insert元素后的aList : ", aList)print("aList pop出的元素: ", aList.pop())aList.remove('ShowMeAI')print("aList用remove删除第1个匹配到的ShowMeAI后: ", aList)aList.reverse()print("aList应用reverse逆序后的后果: ", aList)aList.sort()print("aList应用sort排序后的后果: ", aList)
后果
aList : ['Baidu', 'ShowMeAI', 'google']通过append和+计算后的aList : ['Baidu', 'ShowMeAI', 'google', 'ByteDance', 'ShowMeAI']统计ShowMeAI个数 : 2通过extend后的aList : ['Baidu', 'ShowMeAI', 'google', 'ByteDance', 'ShowMeAI', 'Taobao', 'Tencent']应用index查找Taobao的索引地位: 5在index为3的地位insert元素后的aList : ['Baidu', 'ShowMeAI', 'google', 'DiDi', 'ByteDance', 'ShowMeAI', 'Taobao', 'Tencent']aList pop出的元素: TencentaList用remove删除第1个匹配到的ShowMeAI后: ['Baidu', 'google', 'DiDi', 'ByteDance', 'ShowMeAI', 'Taobao']aList应用reverse逆序后的后果: ['Taobao', 'ShowMeAI', 'ByteDance', 'DiDi', 'google', 'Baidu']aList应用sort排序后的后果: ['Baidu', 'ByteDance', 'DiDi', 'ShowMeAI', 'Taobao', 'google']
8.视频教程
请点击到B站查看【双语字幕】版本
材料与代码下载
本教程系列的代码能够在ShowMeAI对应的github中下载,可本地python环境运行,能迷信上网的宝宝也能够间接借助google colab一键运行与交互操作学习哦!
本教程系列波及的Python速查表能够在以下地址下载获取:
- Python速查表
拓展参考资料
- Python教程—Python3文档
- Python教程-廖雪峰的官方网站
ShowMeAI相干文章举荐
- python介绍
- python装置与环境配置
- python根底语法
- python根底数据类型
- python运算符
- python条件管制与if语句
- python循环语句
- python while循环
- python for循环
- python break语句
- python continue语句
- python pass语句
- python字符串及操作
- python列表
- python元组
- python字典
- python汇合
- python函数
- python迭代器与生成器
- python数据结构
- python模块
- python文件读写
- python文件与目录操作
- python谬误与异样解决
- python面向对象编程
- python命名空间与作用域
- python工夫和日期
ShowMeAI系列教程举荐
- 图解Python编程:从入门到精通系列教程
- 图解数据分析:从入门到精通系列教程
- 图解AI数学根底:从入门到精通系列教程
- 图解大数据技术:从入门到精通系列教程