資訊內容
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.com
2、使用clear()方法ZZl少兒編程網-https://www.pxcodes.com
clear() 方法從列表中刪除所有元素。ZZl少兒編程網-https://www.pxcodes.com
語法ZZl少兒編程網-https://www.pxcodes.com
list.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.com
List清空前: [6, 0, 4, 1] List清空后: []相關推薦:Python3視頻教程
以上就是python中怎么將列表的數據清空的詳細內容,更多請關注少兒編程網其它相關文章!ZZl少兒編程網-https://www.pxcodes.com

- 上一篇
python中int的用法是什么
簡介python中int的用法:【int()】函數用于將一個字符串或數字轉換為整型,語法為【classint(x,base=10)】,參數x表示字符串或數字,base表示進制數,默認十進制。本教程操作環境:windows7系統、python3.9版,DELLG3電腦,該方法適用于所有品牌電腦。pytho
- 下一篇
如何降低python版本
簡介降低python版本的方法:1、打開anacondaprompt窗口;2、輸入“condainstallpython=3.6”命令并回車;3、等待提示(y/n),輸入y,再等待一段時間,當提示done則成功退回到python3.6。本教程操作環境:windows7系統、python3版,DELLG3