list clear 2 python,python中怎么将列表的数据清空
python中將列表數據清空的方法:1、使用del關鍵字,語法格式“del list[:]”;2、使用clear()方法,該方法用于從列表中刪除所有元素,語法格式“list.clear()”。ZZl少兒編程網-https://www.pxcodes.com
ZZl少兒編程網-https://www.pxcodes.com
本教程操作環境:windows7系統、python3版,DELL G3電腦ZZl少兒編程網-https://www.pxcodes.com
python中將列表數據清空的方法ZZl少兒編程網-https://www.pxcodes.com
1、使用del關鍵字ZZl少兒編程網-https://www.pxcodes.com
del可以用來清除范圍中的列表元素,如果我們不給出范圍,則刪除所有元素,做到清空列表。ZZl少兒編程網-https://www.pxcodes.com#!/usr/bin/python
List1 = [8, 6, 16]
List2 = [4, 5, 7]
print ("List1清空前 : " + str(List1))
#使用del刪除List1
del List1[:]
print("List1清空后:" + str(List1))
print('
')
print ("List2清空前 : " + str(List2))
#使用del刪除List2
del List2[:]
print("List2清空后:" + str(List2))
輸出:
ZZl少兒編程網-https://www.pxcodes.comList1清空前 : [8, 6, 16]
List1清空后:[]
List2清空前 : [4, 5, 7]
List2清空后:[]
2、使用clear()方法ZZl少兒編程網-https://www.pxcodes.com
clear() 方法從列表中刪除所有元素。ZZl少兒編程網-https://www.pxcodes.com
語法ZZl少兒編程網-https://www.pxcodes.comlist.clear()
代碼示例:ZZl少兒編程網-https://www.pxcodes.com#!/usr/bin/python
List = [6, 0, 4, 1]
print('List清空前:', List)
#清空元素
List.clear()
print('List清空后:', List)
輸出:ZZl少兒編程網-https://www.pxcodes.comList清空前: [6, 0, 4, 1]
List清空后: []
相關推薦:Python3視頻教程
總結
以上是生活随笔為你收集整理的list clear 2 python,python中怎么将列表的数据清空的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java删除不,Java文件不能删除,该
- 下一篇: websocket python爬虫_p