• python list有关remove的问题


    在python 中进行一次简单的列表循环,当用到remove时出现了一个很有趣的现象,

    代码如下:

    1 a=range(30)
    2 for i in a :
    3     if i%4!=0:
    4         a.remove(i)

    这段代码是在a里取i 当i不能够整除4 的时候,a就在自己里面删除这个时候的i 值,最后输出的a的结果应该是[0,4,8,12,16,20,24,28],结果真的是这样吗? 

    1 a=range(30)
    2 for i in a :
    3     if i%4!=0:
    4         a.remove(i)
    5         
    6 a
    7 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28]

    这个时候出现的结果完全不一样,2,6,10这些根本不对,那么为什么啊?那么这里就得说一下for in和remove了,

    For in 是对下标进行操作,而remove是对值进行操作

    当你执行第一个的时候,a 里面的第一个数是0 现在不符合规则,然后跳过,进行下一个循环,第二个数是1, 符合规则,把a里的1删除,现在a的表成了

    [0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]然该第二个了,因为for in是按照下标取值的,那么第二个就成了3而不是2,这个时候就会把2跳过,以后的类似,而remove是对值进行操作,那么你i传到下面什么值,它就会删除什么值。

    那么怎么该解决这样的问题呢??下面我列出了几种解决的办法,当然方法也不限于这几种,代码如下:

     1 #!/usr/bin/env python
     2 # -*- coding: utf-8 -*-
     3 # Time    : 2017-06-25 18:04
     4 # Author  : MrFiona
     5 # File    : summary_func.py
     6 # Software: PyCharm Community Edition
     7 
     8 import time
     9 
    10 # TODO 4、list的remove的多次操作
    11 
    12 # TODO 第一种处理方法
    13 def list_remove_func_1():
    14     test_remove = [652, 34, 652, 652, 418, 0, 440, 220, 652, 49, 336, 493, 510, 255, 652, 652, [1, 2], 652, 652]
    15     # TODO 用来标记移除的满足条件的数据的个数
    16     remove_num = 0
    17     for index in range(len(test_remove) + 1):
    18         if remove_num:
    19             # TODO 因为移除remove_num了个数据,所以test_remove数据已经减少了相应数量
    20             index -= (remove_num)
    21         try:
    22             # print 'index:	', index, test_remove[index]
    23             if 652 == test_remove[index]:
    24                 n = test_remove[index]
    25                 test_remove.remove(n)
    26                 remove_num += 1
    27                 # print 'remove index [ %d ]	value: [ %d ]' % (index, test_remove[index])
    28         # TODO 因为循环次数开始已经确定为len(test_remove),随着不断的移除数据,test_remove长度在减少,所以当捕捉到
    29         # TODO 的IndexError的时候处理已经完成
    30         except IndexError:
    31             print test_remove
    32 
    33 
    34 # TODO 第二种处理方法
    35 def list_remove_func_2():
    36     test_remove = [652, 34, 652, 652, 418, 0, 440, 220, 652, 49, 336, 493, 510, 255, 652, 652, [1, 2], 652, 652]
    37     # TODO 使用列表表达式来完成,速度增加了五六倍
    38     test_remove = [ ele for ele in test_remove if ele != 652 ]
    39     # TODO 改成元祖表达式又会比列表表达式快两倍
    40     test_remove = ( ele for ele in test_remove if ele != 652 )
    41     print test_remove
    42 
    43 
    44 # TODO 第三种处理方法
    45 def list_remove_func_3():
    46     test_remove = [652, 34, 652, 652, 418, 0, 440, 220, 652, 49, 336, 493, 510, 255, 652, 652, [1, 2], 652, 652]
    47     # TODO 移动次数比正常的移动次数相等,因为是在不停的检测是否还有待移除的数据,有酒移除第一个被发现的符合的数据,否则ValueError退出
    48     time_count = 0
    49     try:
    50         while 1:
    51             test_remove.remove(652)
    52             time_count += 1
    53     except ValueError:
    54         pass
    55     print time_count
    56 
    57 """
    58     TODO Summary:
    59     以移除列表1000000次为准以下几种方法所耗时间对比:
    60         1、list_remove_func_2中用元祖来处理:1.5452-----最快的方法
    61         2、list_remove_func_2中用列表来处理:3.2566-----仅次于元祖的方法
    62         3、list_remove_func_3方法:5.6115-----比较慢的方法
    63         4、list_remove_func_1方法:12.6582-----最慢的方法
    64 """
  • 相关阅读:
    先做人,再做事
    当ligerui的grid出现固定列与非固定列不在同一水平线上时,改怎么处理
    权限设计的idea
    ligerUI问题
    在程序出现问题,当找不到错误时,第一时间用try ,catch包括起来
    当页面是本地页面时,通过ajax访问tomcat里的action,传递的参数在action里并不能识别
    好句子
    js Uncaught TypeError: undefined is not a function
    Photoshop学习笔记(一)
    microsoft project 出现不能保存为xls文件时可以按照如下方法解决
  • 原文地址:https://www.cnblogs.com/MrFiona/p/7078602.html
Copyright © 2020-2023  润新知