Python 列表的使用
(一)列表的定义
1.列表是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。
(二)列表的作用
1.列表用来存储数据,数据可以是成百上千万个。
2.列表中的数据可以是任意类型,数据之间可以无任何联系。
3.对列表进行的操作可以是遍历,修改,删除,统计元素等等
(三)列表的操作
列表的函数和方法:http://www.runoob.com/python/python-lists.html
1.访问列表元素(指定列表的索引)
# 索引从0开始,而不是从1开始
Fruits=['apple','orange','banana','charry']
print("水果是=="+Fruits[2])
结果演示:水果是==banana
2.遍历整个列表
# 遍历列表所有元素
Fruits=['apple','orange','banana','charry']
for fruit in Fruits:print(fruit)
结果演示:appleorangebananacharry
3.修改列表中的元素
# 修改元素,把orange修改为durian
Fruits=['apple','orange','banana','charry']
for i in range(len(Fruits)):if Fruits[i]=='orange':Fruits[i]='durian'
print(Fruits)
结果演示:['apple', 'durian', 'banana', 'charry']
4.列表添加元素
1.向列表末尾添加元素
# 向列表末尾添加元素durian
Fruits=['apple','orange','banana','charry']
Result=Fruits.append('durian')
print(Fruits)
结果演示:['apple', 'orange', 'banana', 'charry', 'durian']
2.在列表任意位置添加元素
# 在orange后面添加durian
Fruits=['apple','orange','banana','charry']
for i in range(len(Fruits)):if Fruits[i]=='orange':Fruits.insert(i+1,'durian')
print(Fruits)
结果演示:['apple', 'orange', 'durian', 'banana', 'charry']
5.删除列表中的元素
1.使用list中的remove(self,object)方法
# 使用remove()方法删除orange
Fruits=['apple','orange','banana','charry']
for fruit in Fruits:if fruit=='orange':Fruits.remove(fruit)
print(Fruits)
结果演示:['apple', 'banana', 'charry']
2.使用del 指定索引删除元素
# 指定索引删除orange
index=int(input("请输入你要删除元素索引:"))
Fruits=['apple','orange','banana','charry']
del Fruits[index]
print(Fruits)
结果演示:请输入你要删除元素索引:1['apple', 'banana', 'charry']
3.使用pop()方法删除指定索引元素
# 指定索引删除orange
Fruits=['apple','orange','banana','charry']
print(Fruits)
#pop()方法默认删除的是末尾元素
#可以改变其索引
poped_Fruit=Fruits.pop()
print(Fruits)
print(poped_Fruit)
结果演示:['apple', 'orange', 'banana', 'charry']['apple', 'orange', 'banana']charry
6.对数子列表进行统计 计算
Items=[5,1,7,6,8,9,3,6,]
print("列表中所有元素的最小值=="+str(min(Items)))
print("列表中所有元素的最大值=="+str(max(Items)))
print("列表中所有元素的和=="+str(sum(Items)))
结果演示:列表中所有元素的最小值==1列表中所有元素的最大值==9列表中所有元素的和==45
7.列表解析
#将1到9的数字平方一次
Squeres=[Value**2 for Value in range(1,10) ]
print("列表中所有元素平方一次的结果:"+str(Squeres))
结果演示:列表中所有元素平方一次的结果:[1, 4, 9, 16, 25, 36, 49, 64, 81]
(四)组织列表
1.使用sort()方法对列表进行永久性排序
# 对列表进行永久排序
Fruits=['apple','orange','banana','charry']
Fruits.sort()
print(Fruits)
结果演示:['apple', 'banana', 'charry', 'orange']
2.使用sorted()函数对列表进行临时排序
# 对列表进行临时排序
Fruits=['apple','orange','banana','charry']
print("原始列表元素排列顺序:\n")
print(Fruits)
print("临时对列表排序为:\n")
print(sorted(Fruits))
print("再次显示原有列表:\n")
print(Fruits)
结果演示:原始列表元素排列顺序:['apple', 'orange', 'banana', 'charry']临时对列表排序为['apple', 'banana', 'charry', 'orange']再次显示原有列表['apple', 'orange', 'banana', 'charry']
3.对列表进行逆向打印
# 对列表进行逆向打印
Fruits=['apple','orange','banana','charry']
print("逆向打印列表元素:")
Fruits.reverse()
print(Fruits)
结果演示:逆向打印列表元素:['charry', 'banana', 'orange', 'apple']
(五)创建数值列表
1.使用rang()函数可以生成一系列的数字
for value in range(1,4):print(value)
结果演示:123
2.使用range()函数创建数值列表的两种方式
方式1
number=[value for value in range(1,10)]
print(""+str(number))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
方式二
number=list(range(1,10))
print("生成的列表:"+str(number))
结果演示:生成的列表:[1, 2, 3, 4, 5, 6, 7, 8, 9]
(六)处理列表的一部分
@切片的基本语法形式
Fruits.[begin:end:step]begin:指从某个位置开始end:结束位置step:表示每隔几个元素在获取元素
1.切片的使用
Fruits=['apple','orange','banana','charry','orange']
print("列表索引为0时:"+str(Fruits[:0]))
print("列表索引为1时"+str(Fruits[:1]))
print("Fruits[0:]等价于Fruits[:]"+str(Fruits[:]))
结果演示:列表索引为0时:[]列表索引为1时['apple']列表索引为2时['apple', 'orange']Fruits[0:]等价于Fruits[:]['apple', 'orange', 'banana', 'charry', 'orange']
2.遍历切面(其实就是遍历列表)
Fruits=['apple','orange','banana','charry','orange']
#冒号前面不写系统默认为0开始
#打印前面3个元素
for fruit in Fruits[:3]:print(fruit)
结果演示:appleorangebanana
3.切片的复制
Fruits=['apple','orange','banana','charry','orange']
#切片的复制
Result=Fruits[3:]
print(Result)
结果演示:['charry', 'orange']
(七)列表中常见得一些问题
1.迭代列表时如何访问列表下标索引
方式一
Fruits=['apple','orange','banana','charry','orange']
for index in range(len(Fruits)):print(index,"--->",Fruits[index])
结果演示:0 ---> apple1 ---> orange2 ---> banana3 ---> charry4 ---> orange
方式二
Fruits=['apple','orange','banana','charry','orange']
for index ,fruit in enumerate(Fruits):print(index,"--->",fruit)注: enumerate()还可以指定从第几个元素开始遍历
结果演示:0 ---> apple1 ---> orange2 ---> banana3 ---> charry4 ---> orange
2.append和extend方法有什么区别?
(一)append表示把某一个数据添加到列表的最后面,添加的参数可以是任意类型。
(二)extend添加的参数必须是一个可迭代对象,表示该对象里面的所有元素一个一个的添加大列表末尾。
1.append实例演示
Fruits=['apple','orange','banana','charry','orange']
Vegetables=['tomato','potato','pumpkin']
Fruits.append(Vegetables)
print(Fruits)
结果演示:['apple', 'orange', 'banana', 'charry', 'orange', ['tomato', 'potato', 'pumpkin']]注:把一个 Vegetables列表整体当做Fruit中的一个元素
2.extend实例演示
Fruits=['apple','orange','banana','charry','orange']
Vegetables=['tomato','potato','pumpkin']
Fruits.extend(Vegetables)
#等价于Fruits+Vegetables
print(Fruits)
结果演示:['apple', 'orange', 'banana', 'charry', 'orange', 'tomato', 'potato', 'pumpkin']注:将Vagetables列表中的一个个元素添加到Fruit列表中。
3.判断列表是否为空的三种方式
Fruits=['apple','orange','banana','charry','orange']
#方式一
if Fruits==[]:print("Fruits列表为空")
#方式二
if len(Fruits)==0:print("Fruits列表为空")
#方式三
if not Fruits:print("Fruits列表为空")
4.随机获取列表中的某个元素
import random
Fruits=['apple','orange','banana','charry','orange']
print("列表随机数为:"+random.choice(Fruits))
结果演示:列表随机数为:apple
Python 列表的使用相关推荐
- python列表(数组)
python列表(数组) 列表(list) 就是 数组 - 列表是Python中的一个对象 - 对象(object)就是内存中专门用来存储数据的一块区域 - 之前我们学习的对象,像数值,它只能保存一 ...
- insert 语句的选择列表包含的项多于插入列表中的项_如何定义和使用Python列表(Lists)
Python中最简单的数据集合是一个列表(list).列表是方括号内用逗号分隔的任何数据项列表.通常,就像使用变量一样,使用=符号为Python列表分配名称. 如果列表中包含数字,则不要在其周围使用引 ...
- python列表嵌套字典取值_Python基础语法:你不得不知的几种变量类型
(点击上方快速关注并设置为星标,一起学Python) 作者:kina_chen來源:简书 01. Python编码Python中默认的编码格式是 ASCII 格式,在没修改编码格式时无法正确打印汉字, ...
- python列表的实现原理_Python列表对象实现原理
Python 列表对象实现原理 Python 中的列表基于 PyListObject 实现,列表支持元 素的插入.删除.更新操作,因此 PyListObject 是一个变长 对象(列表的长度随着元素的 ...
- Python 列表、字典、元组的一些小技巧
1. 字典排序 我们知道 Python 的内置 dictionary 数据类型是无序的,通过 key 来获取对应的 value.可是有时我们需要对 dictionary 中的 item 进行排序输出, ...
- python列表(list)+索引切片+修改+插入+删除+range函数生成整数列表对象
python列表(list)+索引切片+修改+插入+删除+range函数生成整数列表对象 列表(list)是什么? 列表是Python中内置有序.可变序列,列表的所有元素放在一对中括号"[] ...
- python列表(list)中出现次数最多的元素使用collection包的Counter方法
python列表(list)中出现次数最多的元素使用collection包的Counter方法 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tup ...
- python 列表、字典转json字符串
python 列表.字典转json字符串 代码 import json data1 = [ { 'a' : 1, 'b' : 2, 'c' : 3, 'd' : 4, 'e' : 5 } ] data ...
- python列表字典操作_Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结...
创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_star ...
- python读取字符串的list dict_转:Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结...
1 创建列表2 sample_list = ['a',1,('a','b')]3 4 Python 列表操作5 sample_list = ['a','b',0,1,3]6 7 得到列表中的某一个值8 ...
最新文章
- 剑指offer: 面试题40. 最小的k个数
- 【6】青龙面板系列教程之xdd-plus与nolanjdc的对接
- 内核电源管理器已启动关机转换_Linux系统启动流程
- java例7_在Java 7中处理周数
- matlab电力系统潮流计算,大神们,求个电力系统潮流计算的matlab程序。
- 2021年中国动态内容交付市场趋势报告、技术动态创新及2027年市场预测
- 微型计算机的软件系统分为哪几类,系统软件分为哪几类?各有什么特点?
- Python连接SQL Server数据获取2
- MotionEstimate运动估计综述
- 基于Chrome内核(WebKit.net)定制开发DoNet浏览器
- 杨诚 湖南科技职业技术学院计算机,湖南科技大学计算机科学与工程学院
- Pytorch入门教程学习笔记(六)循环神经网络RNN(学周杰伦写歌)
- WawaKM:关于批量抓图的需求分析及设计
- QML编写自定义控件:手风琴图片滑动
- 宣布造车后股价上演“过山车”,开心汽车如意算盘恐落空
- Win11的22H2依然没有WSA(Windows Subsystem for Android)?
- English # 英语学习第一天(audience)
- 机器学习工程师 - Udacity 项目:实现一个狗品种识别算法App
- 宿迁高考成绩查询2021,2021宿迁市地区高考成绩排名查询,宿迁市高考各高中成绩喜报榜单...
- 【数据挖掘】数据挖掘总结 ( 贝叶斯分类器示例 ) ★
热门文章
- PySpark SQL 相关知识介绍
- GO语言————5.4 for 结构
- Skype 2.0.0.63 for Linux
- 1011 A+B 和 C (15 分)c++
- mac_phy_网卡_网口.docx
- 【冰糖Python】RuntimeWarning: invalid value encountered in true_divide
- 论文阅读:Salient Object Detection: A Benchmark
- Unity Inspector添加自定义按钮(Button)
- 计算平均值,统计大于平均值的整数个数
- 豌豆荚应用市场上传时提示“抽取icon失败”解决方案