zoukankan      html  css  js  c++  java
  • ⚠️ Python 循环列表删除元素的注意事项

     错误示范:

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

    解决方式:

    ① 使用尾递归方式

     1 class Solution:
     2     def removeElement(self, nums, val: int) -> int:
     3         for i, num in enumerate(nums[::-1]):
     4             print('i=', i, ', num=', num, ', nums=', nums)
     5             if num == val:
     6                 nums.remove(val)
     7         return len(nums)
     8 
     9 s = Solution()
    10 s.removeElement([2, 0,1,2,2,3,0,4,2], 2)
    11 # i= 0 , num= 2 , nums= [2, 0, 1, 2, 2, 3, 0, 4, 2]
    12 # i= 1 , num= 4 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
    13 # i= 2 , num= 0 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
    14 # i= 3 , num= 3 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
    15 # i= 4 , num= 2 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
    16 # i= 5 , num= 2 , nums= [0, 1, 2, 3, 0, 4, 2]
    17 # i= 6 , num= 1 , nums= [0, 1, 3, 0, 4, 2]
    18 # i= 7 , num= 0 , nums= [0, 1, 3, 0, 4, 2]
    19 # i= 8 , num= 2 , nums= [0, 1, 3, 0, 4, 2]
    View Code

     ② 使用 while 循环的方式

     1 class Solution:
     2     def removeElement(self, nums, val: int) -> int:
     3         i = 0
     4         while i < len(nums):
     5             print('i=', i, ', num=', nums[i], ', nums=', nums)
     6             if nums[i] == val:
     7                 nums.pop(i)
     8             else:
     9                 i += 1
    10         return len(nums)
    11 
    12 s = Solution()
    13 s.removeElement([2, 0,1,2,2,3,0,4,2], 2)
    14 # i= 0 , num= 2 , nums= [2, 0, 1, 2, 2, 3, 0, 4, 2]
    15 # i= 0 , num= 0 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
    16 # i= 1 , num= 1 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
    17 # i= 2 , num= 2 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
    18 # i= 2 , num= 2 , nums= [0, 1, 2, 3, 0, 4, 2]
    19 # i= 2 , num= 3 , nums= [0, 1, 3, 0, 4, 2]
    20 # i= 3 , num= 0 , nums= [0, 1, 3, 0, 4, 2]
    21 # i= 4 , num= 4 , nums= [0, 1, 3, 0, 4, 2]
    22 # i= 5 , num= 2 , nums= [0, 1, 3, 0, 4, 2]
    View Code

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

     1 class Solution:
     2     def removeElement(self, nums, val: int) -> int:
     3         for i, num in enumerate(nums[:]):
     4             print('i=', i, ', num=', num, ', nums=', nums)
     5             if num == val:
     6                 nums.remove(val)
     7         return len(nums)
     8 
     9 s = Solution()
    10 s.removeElement([2, 0,1,2,2,3,0,4,2], 2)
    11 # i= 0 , num= 2 , nums= [2, 0, 1, 2, 2, 3, 0, 4, 2]
    12 # i= 1 , num= 0 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
    13 # i= 2 , num= 1 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
    14 # i= 3 , num= 2 , nums= [0, 1, 2, 2, 3, 0, 4, 2]
    15 # i= 4 , num= 2 , nums= [0, 1, 2, 3, 0, 4, 2]
    16 # i= 5 , num= 3 , nums= [0, 1, 3, 0, 4, 2]
    17 # i= 6 , num= 0 , nums= [0, 1, 3, 0, 4, 2]
    18 # i= 7 , num= 4 , nums= [0, 1, 3, 0, 4, 2]
    19 # i= 8 , num= 2 , nums= [0, 1, 3, 0, 4, 2]
    View Code

  • 相关阅读:
    互联网秒杀设计
    生产者消费者模式实现
    Ping CAP CTO、Codis作者谈redis分布式解决方案和分布式KV存储
    VIM使用学习笔记 : 按键说明
    Cookie的有效访问路径
    简单的Cookie记录浏览记录案例
    认识Cookie和状态管理
    Java异常
    Java接口基础
    String常用方法
  • 原文地址:https://www.cnblogs.com/catyuang/p/11121663.html
Copyright © 2011-2022 走看看