当前位置:首页 > python

列表的删除方法练习题

admin10个月前 (01-26)python8118
# 练习: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://www.kuangmin.top/post/100.html

分享给朋友:

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

dp爬起点中文网

from DrissionPage import WebPage page = WebPage() url = 'https://www.qidian.com/chapter/1036370336/74597756…

dp爬boss直聘

# url='https://www.zhipin.com/web/geek/job?query=python&city=101010100&page=1' from DrissionPage import ChromiumP…

python读文件操作

# 打开名为 '111.txt' 的文件,以只读模式读取,文件编码为 utf-8 with open('111.txt', 'r', encoding='utf-8'…

《Python入门100题》之第二题:范围数字查找

编写一个程序:查找所有此类数字,它们可以被7整除,但不能是5的倍数在(2000和3200之间)均包含在内list1 = [] for i in range(2000, 3201):     if&…

《Python入门100题》之第五题:计算阶乘的和

题目:求1+2!+3!+.....+20!的和.# 题目:求1+2!+3!+.....+20!的和. ji = 1 sum = 0 for i in range(1, 20 + 1):…

判断一个数字是不是素数

#  方案一,这是第一感觉写的 num = int(input('输入一个数字:')) for i in range(2, num):     if …