Python 创建空的list,以及append用法讲解
作者:yebahe 时间:2021-02-23 17:05:58
Python中list的用法:如何创建list,如何表达list中的元素,如何修改和删除list
运行环境:Python 3.6.2
0.空list的创建:
l = list()
或者:
l = []
1.list中元素的创建和表达
fruits = ['apple', 'banana', 'pear', 'grapes', 'pineapple', 'watermelon']
fruits[2] #从0开始数起,第三个元素
pear
2.list中元素的更改
fruits[2] = 'tomato'
print(fruits)
['apple', 'banana', 'tomato', 'grapes', 'pineapple', 'watermelon']
3.在list末尾增加更多元素
fruits.append('eggplant')
print(fruits)
['apple', 'banana', 'tomato', 'grapes', 'pineapple', 'watermelon', 'eggplant']
4.如何截取list中的某一段
print(fruit[: 2]) #从list的首元素开始截取,截取到位置'3',但不包括第3个元素
['apple', 'banana']
5. 如何更改list中连续的元素
fruits[:2] = ['a', 'b']
print(fruits)
['a', 'b', 'tomato', 'grapes', 'pineapple', 'watermelon', 'eggplant']
6.如何删除list中某段元素,或者全部list
fruits[:2] = [] #删除前两个元素
print(fruits)
['tomato', 'grapes', 'pineapple', 'watermelon', 'eggplant']
fruits[:] = [] #删除全部list元素
[]
来源:https://blog.csdn.net/u012486361/article/details/77073020
标签:python,list,append
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于H1的用法探讨
2008-03-18 12:55:00
Pycharm最常用的快捷键及使用技巧
2022-12-13 11:33:25
![](https://img.aspxhome.com/file/2023/6/75116_0s.png)
php+mysqli实现批量替换数据库表前缀的方法
2023-11-22 10:15:55
![](https://img.aspxhome.com/file/2023/2/94722_0s.jpg)
Python实现圣诞树的多种方法
2023-07-27 01:02:20
![](https://img.aspxhome.com/file/2023/7/61477_0s.jpg)
使用python socket分发大文件的实现方法
2022-05-23 10:45:40
python机器学习pytorch 张量基础教程
2023-06-18 04:54:31
![](https://img.aspxhome.com/file/2023/4/65094_0s.png)
Python装饰器有哪些绝妙的用法
2022-09-08 01:09:47
IBatis.net连接MySql 问题与故障的解决方案
2011-06-02 09:38:00
Python 多线程爬取案例
2022-11-17 16:02:06
![](https://img.aspxhome.com/file/2023/6/90546_0s.png)
asp如何用FSO对象显示一个文本文件?
2010-06-09 18:41:00
Python之lxml安装失败的解决
2023-11-27 04:34:22
![](https://img.aspxhome.com/file/2023/5/82705_0s.png)
详解Python如何实现发送带附件的电子邮件
2022-03-20 13:33:46
![](https://img.aspxhome.com/file/2023/2/64742_0s.png)
python 多进程队列数据处理详解
2022-04-10 23:49:44
MySQL安全性指南 (2)
2010-07-26 13:26:00
2008圣诞节网站特色Logo不完全点评
2008-12-25 18:35:00
![](https://img.aspxhome.com/file/UploadPic/up/1-shengdanjie.jpg)
Python 数值区间处理_对interval 库的快速入门详解
2022-08-12 00:18:53
如何获得上一个月份是几月?
2009-11-23 20:38:00
举例详解Python中smtplib模块处理电子邮件的使用
2023-10-08 04:46:14
开心网上input输入框研究
2009-03-06 12:52:00
![](https://img.aspxhome.com/file/UploadPic/20093/6/1207fc9f5fag215-89s.jpg)
PHP5.6读写excel表格文件操作示例
2023-11-21 15:03:21