“Python/gloss python remove list items”的版本间差异
来自菜鸟教程
小 (Pywikibot 4.4.0.dev0) |
小 (机器人:添加分类Python基础教程) |
||
第105行: | 第105行: | ||
</div> | </div> | ||
<br /> | <br /> | ||
+ | |||
+ | [[分类:Python基础教程]] |
2020年10月29日 (四) 08:48的最新版本
<languages />
Python删除列表项
删除列表项
有几种方法可以从列表中删除项目:
例
The
remove()
方法删除指定的项目:
thislist = ["apple", "banana", "cherry"] thislist.remove("banana") print(thislist)
例
The
pop()
方法将删除指定的索引(如果未指定index,则删除最后一项):
thislist = ["apple", "banana", "cherry"] thislist.pop() print(thislist)
例
The
del
关键字删除指定的索引:
thislist = ["apple", "banana", "cherry"] del thislist[0] print(thislist)
例
The
del
关键字也可以完全删除列表:
thislist = ["apple", "banana", "cherry"] del thislist
例
The
clear()
方法清空列表:
thislist = ["apple", "banana", "cherry"] thislist.clear() print(thislist)