python中怎么将列表的数据清空


本文摘自php中文网,作者青灯夜游,侵删。

python中将列表数据清空的方法:1、使用del关键字,语法格式“del list[:]”;2、使用clear()方法,该方法用于从列表中删除所有元素,语法格式“list.clear()”。

本教程操作环境:windows7系统、python3版,DELL G3电脑

python中将列表数据清空的方法

1、使用del关键字

del可以用来清除范围中的列表元素,如果我们不给出范围,则删除所有元素,做到清空列表。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

#!/usr/bin/python

List1 = [8, 6, 16]

List2 = [4, 5, 7]

 

print ("List1清空前 : " + str(List1))

#使用del删除List1

del List1[:]

print("List1清空后:" + str(List1)) 

 

print('\n')

  

print ("List2清空前 : " + str(List2))

#使用del删除List2

del List2[:]

print("List2清空后:" + str(List2))

输出:

1

2

3

4

5

List1清空前 : [8, 6, 16]

List1清空后:[]

 

List2清空前 : [4, 5, 7]

List2清空后:[]

2、使用clear()方法

clear() 方法从列表中删除所有元素。

语法

1

list.clear()

代码示例:

1

2

3

4

5

6

7

#!/usr/bin/python

List = [6, 0, 4, 1]

print('List清空前:', List) 

   

#清空元素

List.clear() 

print('List清空后:', List)

输出:

1

2

List清空前: [6, 0, 4, 1]

List清空后: []

相关推荐:Python3视频教程

以上就是python中怎么将列表的数据清空的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

怎么下载官网Python并安装

Python继承的代码示例

Python不可变数据类型有哪些

Python中reverse、sort、sorted三个列表排序使用方法详解

8年后Python重登2018年度编程语言王座

Python内置变量的一个例子

Python有什么用

Python中或者怎么表示

pyquery解析器无法获取标签名dom节点的解决方法

Python实现获得当前目录以及上级目录的方法

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...