当前位置:首页 > python

列表的删除方法练习题

admin7天前python624
# 练习:list1 = [1,2,3,4,5,5,4,3,2,1,1,2,3,4,5]
# 需求:使用 remove 删除所有的 5
list1 = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5]
count = list1.count(5)
for i in range(count):
    list1.remove(5)
print(list1)

# 需求:使用 pop 删除所有的 5
# list1 = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5]
# for i in range(len(list1) - 1, -1, -1):
#     if list1[i] == 5:
#         list1.pop(i)
#
# print(list1)
# 需求:使用 del 删除所有的 5
# list1 = [1, 2, 3, 4, 5, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5]
# for i in range(len(list1) - 1, -1, -1):
#     if list1[i] == 5:
#         del list1[i]
# 
# print(list1)


扫描二维码推送至手机访问。

版权声明:本文由匡民博客发布,如需转载请注明出处。

本文链接:https://kuangmin.top/post/100.html

分享给朋友:

“列表的删除方法练习题” 的相关文章

DP助手

SaossionPage 发行版 - Gitee.com…

dp查找iframe元素

from DrissionPage import ChromiumPage dp = ChromiumPage() # # 同域的直接拿 dp.get('https://www.qiju.cc/vod/play/…

制作自动答题脚本教程

基本的操作,联系下不错Python DrissionPage 制作自动答题脚本_哔哩哔哩_bilibili…

ddddocr的安装使用

pip install -i https://mirrors.bfsu.edu.cn/pypi/web/simple/  ddddocrfrom DrissionPage import ChromiumPage imp…

dp爬同花顺

from DrissionPage import ChromiumPage dp = ChromiumPage() dp.get('https://data.10jqka.com.cn/rank/cxg/') for&nbs…

列表的删除

# 列表的删除 fans = ['zhangsan', 'lisi', 'wangwu'] # 根据索引来分:删除最后一个元素,区别就是pop有返回被删的元素,del没有返回值 #&nb…