错误示范:

 class Solution:
def removeElement(self, nums, val: int) -> int:
for i, num in enumerate(nums):
print('i=', i, ', num=', num, ', nums=', nums)
if num == val:
nums.remove(val)
return len(nums) s = Solution()
s.removeElement([2, 0,1,2,2,3,0,4,2], 2)
# i= 0 , num= 2 , nums= [2, 0, 1, 2, 2, 3, 0, 4, 2]
# i= 1 , num= 1 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 2 , num= 2 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 3 , num= 3 , nums= [0, 1, 2, 3, 0, 4, 2]
# i= 4 , num= 0 , nums= [0, 1, 2, 3, 0, 4, 2]
# i= 5 , num= 4 , nums= [0, 1, 2, 3, 0, 4, 2]
# i= 6 , num= 2 , nums= [0, 1, 2, 3, 0, 4, 2]

解决方式:

① 使用尾递归方式

 class Solution:
def removeElement(self, nums, val: int) -> int:
for i, num in enumerate(nums[::-1]):
print('i=', i, ', num=', num, ', nums=', nums)
if num == val:
nums.remove(val)
return len(nums) s = Solution()
s.removeElement([2, 0,1,2,2,3,0,4,2], 2)
# i= 0 , num= 2 , nums= [2, 0, 1, 2, 2, 3, 0, 4, 2]
# i= 1 , num= 4 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 2 , num= 0 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 3 , num= 3 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 4 , num= 2 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 5 , num= 2 , nums= [0, 1, 2, 3, 0, 4, 2]
# i= 6 , num= 1 , nums= [0, 1, 3, 0, 4, 2]
# i= 7 , num= 0 , nums= [0, 1, 3, 0, 4, 2]
# i= 8 , num= 2 , nums= [0, 1, 3, 0, 4, 2]

② 使用 while 循环的方式

 class Solution:
def removeElement(self, nums, val: int) -> int:
i = 0
while i < len(nums):
print('i=', i, ', num=', nums[i], ', nums=', nums)
if nums[i] == val:
nums.pop(i)
else:
i += 1
return len(nums) s = Solution()
s.removeElement([2, 0,1,2,2,3,0,4,2], 2)
# i= 0 , num= 2 , nums= [2, 0, 1, 2, 2, 3, 0, 4, 2]
# i= 0 , num= 0 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 1 , num= 1 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 2 , num= 2 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 2 , num= 2 , nums= [0, 1, 2, 3, 0, 4, 2]
# i= 2 , num= 3 , nums= [0, 1, 3, 0, 4, 2]
# i= 3 , num= 0 , nums= [0, 1, 3, 0, 4, 2]
# i= 4 , num= 4 , nums= [0, 1, 3, 0, 4, 2]
# i= 5 , num= 2 , nums= [0, 1, 3, 0, 4, 2]

③ 对整个序列使用切片来创建一个临时副本

 class Solution:
def removeElement(self, nums, val: int) -> int:
for i, num in enumerate(nums[:]):
print('i=', i, ', num=', num, ', nums=', nums)
if num == val:
nums.remove(val)
return len(nums) s = Solution()
s.removeElement([2, 0,1,2,2,3,0,4,2], 2)
# i= 0 , num= 2 , nums= [2, 0, 1, 2, 2, 3, 0, 4, 2]
# i= 1 , num= 0 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 2 , num= 1 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 3 , num= 2 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
# i= 4 , num= 2 , nums= [0, 1, 2, 3, 0, 4, 2]
# i= 5 , num= 3 , nums= [0, 1, 3, 0, 4, 2]
# i= 6 , num= 0 , nums= [0, 1, 3, 0, 4, 2]
# i= 7 , num= 4 , nums= [0, 1, 3, 0, 4, 2]
# i= 8 , num= 2 , nums= [0, 1, 3, 0, 4, 2]

Python 循环列表删除元素的注意事项的更多相关文章

  1. Python循环列表删除元素问题

    有人会遇到这种问题,遍历列表,想删除列表中的某几个元素,执行后发现有些并没有删除到, 比如以下代码 a=[1,2,3,4,5,6]print(a) for i in a: if i==3 or i== ...

  2. Python list列表修改元素

    Python 提供了两种修改列表(list)元素的方法,你可以每次修改单个元素,也可以每次修改一组元素(多个). 修改单个元素 修改单个元素非常简单,直接对元素赋值即可.请看下面的例子: nums = ...

  3. Python循环列表的方法

    python循环列表的几种方法: 第一,依次打印列表中的各项值. 1 #!usr/bin/env python3 2 #!-*- Coding:utf-8 -*- 3 4 ''' 5 多种循环列表的方 ...

  4. python中列表删除和多重循环退出

    在学习python的时候,会有一些梗非常不适应,在此列举列表删除和多重循环退出的例子: 列表删除里面的坑 比如我们有一个列表里面有很多相同的值,假如:nums=[1,6,6,3,6,2,10,2,10 ...

  5. Python遍历列表删除多个列表元素

    在遍历list的时候,删除符合条件的数据,结果不符合预期 num_list = [1, 2, 2, 2, 3] print(num_list) for item in num_list: if ite ...

  6. python遍历列表删除多个元素的坑

    如下代码,遍历列表,删除列表中的偶数时,结果与预期不符. a = [11, 20, 4, 5, 16, 28] for i in a: if i % 2 == 0: a.remove(i) print ...

  7. Python遍历列表删除多个元素或者重复元素

    在遍历list的时候,删除符合条件的数据,结果不符合预期   num_list = [1, 2, 2, 2, 3] print(num_list) for item in num_list: if i ...

  8. python中列表中元素的增删改查

    增: append : 默认添加到列表的最后一个位置 insert : 可以通过下标添加到列表的任意位置 extend: a.extend[b] --将b列表的元素全加入到列表b中 删; remove ...

  9. javascript在数组的循环中删除元素

    在开发JavaScript应用的过程中,经常会遇到在循环中移除指定元素的需求. 按照常规的思路,就是对数组进行一个for循环,然后在循环里面进行if判断,在判断中删除掉指定元素即可. 但是实际情况往往 ...

随机推荐

  1. mysql 5.6 datetime default now()

    CREATE TABLE `test` (   id int,   `gmt_create` datetime  DEFAULT NOW() not NULL )ENGINE=InnoDB; mysq ...

  2. 035:DTL常用过滤器(4)

    join过滤器: 类似与 Python 中的 join ,将列表/元组/字符串用指定的字符进行拼接.示例代码如下: {{ value|join:"/" }} 如果 value 是等 ...

  3. UVa 10054 : The Necklace 【欧拉回路】

    题目链接 题目大意:我的妹妹有一串由各种颜色组成的项链. 项链中两个连续珠子的接头处共享同一个颜色. 如上图, 第一个珠子是green+red, 那么接这个珠子的必须以red开头,如图的red+whi ...

  4. 使用vue进行国际化

    相对于网站等一些需求 我们有需要做国际化的需求,具体步骤如下: 首先安装 vue-i18n npm install vue-i18n import VueI18n from 'vue-i18n' Vu ...

  5. App中h5音频不能播放问题

    前置:以下问题是针对vue项目的解决方案 问题一:IOS中音频不能自动播放 原因:ios禁止了音频自动播放 解决办法:在vue的生命周期mounted中获取音频Dom并调用音频播放方法play(),注 ...

  6. leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和]

    题目链接 Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square w ...

  7. [CF959E]Mahmoud and Ehab and the xor-MST题解

    解法 又是一道结论题? 我的做法比较奇怪且没有证明 #include <cstdio> #include <cmath> #define ll long long int ma ...

  8. spring4.1.8扩展实战之七:控制bean(BeanPostProcessor接口)

    本章是<spring4.1.8扩展实战>的第七篇,我们来尝试在容器初始化的时候对bean实例做设置: 原文地址:https://blog.csdn.net/boling_cavalry/a ...

  9. 电脑可以识别sd卡手机无法识别 的解决方法。 我成功了。 淘宝买的sd卡 不用退货了。 退的人肝疼

    https://wenku.baidu.com/view/822e471055270722192ef736.html 电脑可以识别 sd 卡手机无法识别 * (本教程只是本人实际操作方法,可以解决一部 ...

  10. 怎样用idhttpserver代替IIS让用户浏览html或下载文件 http://bbs.csdn.net/topics/360248674

      怎样用idhttpserver代替IIS让用户浏览html或下载文件 更多0分享到: 相关知识库: C# 虚拟现实(VR) Node.js 算法与数据结构     对我有用[0] 丢个板砖[0]  ...