Python中列表(list)操作方法汇总
作者:shichen2014 时间:2023-11-12 06:16:33
本文实例汇总了Python中关于列表的常用操作方法,供大家参考借鉴。具体方法如下:
一、Python创建列表:
sample_list = ['a',1,('a','b')]
二、Python 列表操作:
假设有如下列表:
sample_list = ['a','b',0,1,3]
1.得到列表中的某一个值:
value_start = sample_list[0]
end_value = sample_list[-1]
2.删除列表的第一个值:
del sample_list[0]
3.在列表中插入一个值:
sample_list[0:0] = ['sample value']
4.得到列表的长度:
list_length = len(sample_list)
5.列表遍历:
for element in sample_list:
print(element)
三、Python 列表高级操作/技巧
1.产生一个数值递增列表:
num_inc_list = range(30)
#will return a list [0,1,2,...,29]
2.用某个固定值初始化列表:
initial_value = 0
list_length = 5
sample_list = [ initial_value for i in range(10)]
sample_list = [initial_value]*list_length
# sample_list ==[0,0,0,0,0]
读者还可以在此基础上继续搜集关于Python列表操作的其他方法,进一步巩固及加深对Python列表操作的认识。
标签:Python,列表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何在Django中使用聚合的实现示例
2021-08-02 10:32:30
Python调用实现最小二乘法的方法详解
2022-07-04 16:31:00
![](https://img.aspxhome.com/file/2023/7/66377_0s.png)
Python抓取通过Ajax加载数据的示例
2023-12-09 21:28:38
减少用户的思考
2010-09-07 12:14:00
![](https://img.aspxhome.com/file/UploadPic/20109/7/01-50s.jpg)
python定义函数如何判断年份是否为闰年
2022-01-29 18:56:12
![](https://img.aspxhome.com/file/2023/1/112041_0s.png)
Python3实现并发检验代理池地址的方法
2023-10-14 01:03:58
pyqt 实现在Widgets中显示图片和文字的方法
2021-12-25 07:28:16
![](https://img.aspxhome.com/file/2023/4/100844_0s.jpg)
Python多线程爬虫实战_爬取糗事百科段子的实例
2021-02-15 16:58:21
利用keras加载训练好的.H5文件,并实现预测图片
2021-01-18 21:18:57
CMS不要让MySQL为你流泪
2008-12-11 14:38:00
js实现多张图片打包成zip
2024-04-22 22:15:09
python实现录屏功能(亲测好用)
2022-01-12 19:54:34
![](https://img.aspxhome.com/file/2023/0/70840_0s.jpg)
Python return函数返回值类型和帮助函数使用教程
2021-01-13 14:21:32
JavaScript实现的背景自动变色代码
2024-04-16 09:02:17
![](https://img.aspxhome.com/file/2023/4/136724_0s.png)
python获取指定时间段内特定规律的日期列表
2021-02-09 02:16:25
![](https://img.aspxhome.com/file/2023/0/76930_0s.png)
mysql 查询数据库中的存储过程与函数的语句
2024-01-26 17:30:35
Go的固定时长定时器和周期性时长定时器
2023-06-27 12:44:23
MGR集群搭建及配置过程
2024-01-24 18:56:14
![](https://img.aspxhome.com/file/2023/9/68599_0s.png)
Sql server中的char、varchar、text和nchar、nvarchar、ntext的区别
2011-08-14 09:43:44
SQL Server 存储过程遇到“表 ''#TT'' 没有标识属性无法执行 SET 操作”错误
2024-01-24 00:07:57
![](https://img.aspxhome.com/file/2023/8/85598_0s.png)