




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Python列表创建列表01
列表(Lists)属于Python中的序列类型,它是任意对象的有序集合,通过“位置”或者“索引”访问其中的元素,它具有可变对象、可变长度、异构和任意嵌套的特点。列表的概述01
列表里第一个元素的“位置”或者“索引”是从“0”开始,第二个元素的则是“1”,以此类推。在创建列表时,列表元素放置在方括号[]中,以逗号来分隔各元素,格式如下:listname=[元素1,元素2,元素3,……,元素n]举例如下:sample_list1=[0,1,2,3,4]sample_list2=["P","y","t","h","o","n"]sample_list3=['Python','sample','list','for','your','reference']列表概述01代码运行如下:>>>sample_list1=[0,1,2,3,4]#列表sample_list1>>>sample_list2=["P","y","t","h","o","n"]#列表sample_list2>>>sample_list3=['Python','sample','list','for','your','reference']#列表sample_list3>>>print(sample_list1)#打印输出列表[0,1,2,3,4]#输出结果>>>print(sample_list2)#打印输出列表['p','y','t','h','o','n']#输出结果>>>print(sample_list3)#打印输出列表['Python','sample','list','for','your','reference']#输出结果列表概述01列表中允许有不同数据类型的元素,例如:sample_list4=[0,"y",2,"h",4,"n",'Python']但通常建议列表中元素最好使用相同的数据类型。列表可以嵌套使用,例如:sample_list5=[sample_list1,sample_list2,sample_list3]运行结果如下:>>>sample_list1=[0,1,2,3,4]>>>sample_list2=["P","y","t","h","o","n"]>>>sample_list3=['Python','sample','list','for','your','reference']>>>sample_list5=[sample_list1,sample_list2,sample_list3]#创建一个嵌套列表>>>print(sample_list5)[[0,1,2,3,4],['P','y','t','h','o','n'],['Python','sample','list','for','your','reference']]使用列表02通过使用“位置”或者“索引”来访问列表中的值,将放在方括号中。特别注意,“位置”或者“索引”是从0开始,例如引用上一节列表示例sample_list1中的第1个,可以写成:sample_list1[0];引用第3个值,可以写成:sample_list1[2]。代码示例为:>>>sample_list1=[0,1,2,3,4]>>>print("sample_list1[0]:",sample_list1[0])#输出索引为0的元素sample_list1[0]:0>>>print("sample_list1[2]:",sample_list1[2])#输出索引为2的元素sample_list1[2]:2使用列表02通过使用“位置”或者“索引”来访问列表中的值,将放在方括号中。特别注意,“位置”或者“索引”是从0开始,例如引用上一节列表示例sample_list1中的第1个,可以写成:sample_list1[0];引用第3个值,可以写成:sample_list1[2]。代码示例为:>>>sample_list1=[0,1,2,3,4]>>>print("sample_list1[0]:",sample_list1[0])#输出索引为0的元素sample_list1[0]:0>>>print("sample_list1[2]:",sample_list1[2])#输出索引为2的元素sample_list1[2]:2使用列表02可以在方括号中使用“负整数”,如:sample_list1[-2],意为从列表的右侧开始倒数2个的元素,即索引倒数第2的元素。>>>sample_list1=[0,1,2,3,4]>>>print("sample_list1[-2]:",sample_list1[-2])#输出索引倒数第2的元素sample_list1[-2]:3使用列表02以在方括号中用冒号分开的两个整数来截取列表中的元素,例如sample_list2[2:4],可取得列表sample_list2中的第3个和第4个元素,不包含第5个元素。>>>sample_list2=["p","y","t","h","o","n"]>>>print("sample_list2[2:4]:",sample_list2[2:4])sample_list2[2:4]:['t','h']该类操作被称为“切片”操作(slice)。使用列表02对列表的元素进行修改时,可以使用赋值语句:>>>sample_list3=['python','sample','list','for','your','reference']>>>sample_list3[4]='my'>>>print("sample_list3[4]:",sample_list3[4])sample_list3[4]:my>>>print("sample_list3:",sample_list3)sample_list3:['python','sample','list','for','my','reference']删除列表元素
03删除列表的元素,可以使用del语句,格式为:dellistname[索引]该索引的元素被删除后,后面的元素将会自动移动并填补该位置。在不知道或不关心元素的索引时,可以使用列表内置方法remove()来删除指定的值,例如:listname.remove('值')清空列表,可以采用重新创建一个与原列表名相同的空列表的方法,例如:listname=[]删除整个列表,也可以使用del语句,格式为:dellistname删除列表元素
03代码示例如下:>>>sample_list4=[0,"y",2,"h",4,"n",'Python']>>>delsample_list4[5]#删除列表中索引为5的元素>>>print("afterdeletion,sample_list4:",sample_list4)afterdeletion,sample_list4:[0,'y',2,'h',4,'Python']>>>sample_list4.remove('Python')#删除列表中值为Python的元素>>>print("afterremoving,sample_list4:",sample_list4)afterremoving,sample_list4:[0,'y',2,'h',4]>>>sample_list4=[]#重新创建列表并置为空>>>print(sample_list4)#输出该列表[]>>>delsample_list4#删除整个列表>>>print(sample_list4)#打印输出整个列表Traceback(mostrecentcalllast):File"<pyshell#108>",line1,in<module>print(sample_list4)NameError:name'sample_list4'isnotdefined#系统报告该列表未定义列表的内置函数与其他方法
04代码示例如下:>>>sample_list1=[0,1,2,3,4]>>>len(sample_list1)#列表的元素数量5>>>max(sample_list1)#列表中元素的最大值4>>>min(sample_list1)#列表中元素的最小值03列表的内置函数与其他方法
043列表的内置函数与其他方法
043方法说明listname.append(元素)添加新的元素在列表末尾listname.count(元素)统计该元素在列表中出现的次数listname.extend(序列)追加另一个序列类型中的多个值,到该列表末尾(用新列表扩展原来的列表)listname.index(元素)从列表中找出某个值第一个匹配元素的索引位置listname.insert(位置,元素)将元素插入列表listname
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 钢材加工安装合同范本
- 合伙创业分红合同范本
- 吉林省吉林市吉林高新技术产业开发区2024-2025学年八年级上学期12月期末考试数学试卷(含解析)
- 雕塑进货出售合同范本
- 砂石土方运输合同范本
- 豆粨采购合同范本
- 《2025租赁合同终极大典》
- 出售球拍合同范本
- 山东省日照市2025届高三下学期一模试题 政治 无答案
- 2024年扬州市广陵区教育系统事业单位招聘教师真题
- 2025-2030年中国CAE软件行业市场行情监测及发展前景研判报告
- 2025江西南昌市江铜产融社会招聘1人笔试参考题库附带答案详解
- (二统)昆明市2025届“三诊一模”高三复习教学质量检测地理试卷(含答案)
- Unit 3 Keep Fit Section A 2a-2e 教学设计 2024-2025学年人教版(2024)七年级英语下册
- 2025徽县辅警考试题库
- (一模)2025年广东省高三高考模拟测试 (一) 卷数学试卷(含官方答案)
- 脑心健康管理师的学习汇报
- 树木移植合同范本
- 2025年开封大学单招职业技能测试题库新版
- 2025年张家界航空工业职业技术学院单招职业技能测试题库及参考答案
- 财政投资评审咨询服务预算和结算评审项目投标文件(技术方案)
评论
0/150
提交评论