python list遍历时删除元素

python list遍历时候删除还真需要注意下,今天帮同学处理数据,竟然傻逼了。

需求:

除了第一列,给每列加一个序号如:"1:0","2:0","3:20100307",然后删除冒号后为0的数据。

推荐做法:

arrays = [
['5001', '0', '0', '20100307', '20150109', '2', '3', '75', '0', '0', '114', '13', '2', '0', '0'],
['10001', '0', '2', '20100318', '20150119', '2', '3', '58', '0', '0', '1', '5', '1', '0', '0'],
['15001', '0', '2', '20100329', '20150130', '9', '14', '70', '0', '0', '1', '31', '7', '11', '7'],
['20001', '0', '0', '20100401', '20150117', '3', '5', '71', '1', '75', '1', '1', '1', '0', '0'],
['22001', '0', '1', '20100401', '20150125', '7', '11', '58', '0', '0', '51', '40', '12', '2', '1'],
['24001', '0', '0', '20100401', '20141209', '1', '1', '138', '0', '0', '1', '1', '1', '0', '0'],
['44001', '1', '0', '20100428', '20141130', '1', '1', '85', '0', '0', '1', '0', '0', '0', '0'],
['45001', '1', '0', '20100429', '20141127', '1', '1', '128', '0', '0', '1', '24', '3', '0', '0'],
['50001', '0', '0', '20100506', '20141230', '1', '2', '67', '0', '0', '44', '6', '1', '4', '1'],
['69001', '0', '0', '20100524', '20150129', '16', '20', '90', '2', '220', '236', '44', '18', '3', '2'],
['71001', '0', '2', '20100526', '20141224', '9', '15', '64', '0', '0', '57', '30', '7', '11', '4']
]

for every_list in arrays:
    #add col number
    for i in range(len(every_list)):
        if i == 0:
            continue
        every_list[i] = str(i)+":"+every_list[i]

    #remove col which value==0
    every_list = filter(lambda e:e.split(":")[-1] != '0' ,every_list)

    print every_list

结果:

['5001', '3:20100307', '4:20150109', '5:2', '6:3', '7:75', '10:114', '11:13', '12:2']
['10001', '2:2', '3:20100318', '4:20150119', '5:2', '6:3', '7:58', '10:1', '11:5', '12:1']
['15001', '2:2', '3:20100329', '4:20150130', '5:9', '6:14', '7:70', '10:1', '11:31', '12:7', '13:11', '14:7']
['20001', '3:20100401', '4:20150117', '5:3', '6:5', '7:71', '8:1', '9:75', '10:1', '11:1', '12:1']
['22001', '2:1', '3:20100401', '4:20150125', '5:7', '6:11', '7:58', '10:51', '11:40', '12:12', '13:2', '14:1']
['24001', '3:20100401', '4:20141209', '5:1', '6:1', '7:138', '10:1', '11:1', '12:1']
['44001', '1:1', '3:20100428', '4:20141130', '5:1', '6:1', '7:85', '10:1']
['45001', '1:1', '3:20100429', '4:20141127', '5:1', '6:1', '7:128', '10:1', '11:24', '12:3']
['50001', '3:20100506', '4:20141230', '5:1', '6:2', '7:67', '10:44', '11:6', '12:1', '13:4', '14:1']
['69001', '3:20100524', '4:20150129', '5:16', '6:20', '7:90', '8:2', '9:220', '10:236', '11:44', '12:18', '13:3', '14:2']
['71001', '2:2', '3:20100526', '4:20141224', '5:9', '6:15', '7:64', '10:57', '11:30', '12:7', '13:11', '14:4']

其实一开始傻逼的做法:

arrays = [
['5001', '0', '0', '20100307', '20150109', '2', '3', '75', '0', '0', '114', '13', '2', '0', '0'],
['10001', '0', '2', '20100318', '20150119', '2', '3', '58', '0', '0', '1', '5', '1', '0', '0'],
['15001', '0', '2', '20100329', '20150130', '9', '14', '70', '0', '0', '1', '31', '7', '11', '7'],
['20001', '0', '0', '20100401', '20150117', '3', '5', '71', '1', '75', '1', '1', '1', '0', '0'],
['22001', '0', '1', '20100401', '20150125', '7', '11', '58', '0', '0', '51', '40', '12', '2', '1'],
['24001', '0', '0', '20100401', '20141209', '1', '1', '138', '0', '0', '1', '1', '1', '0', '0'],
['44001', '1', '0', '20100428', '20141130', '1', '1', '85', '0', '0', '1', '0', '0', '0', '0'],
['45001', '1', '0', '20100429', '20141127', '1', '1', '128', '0', '0', '1', '24', '3', '0', '0'],
['50001', '0', '0', '20100506', '20141230', '1', '2', '67', '0', '0', '44', '6', '1', '4', '1'],
['69001', '0', '0', '20100524', '20150129', '16', '20', '90', '2', '220', '236', '44', '18', '3', '2'],
['71001', '0', '2', '20100526', '20141224', '9', '15', '64', '0', '0', '57', '30', '7', '11', '4']
]

for every_list in arrays:
    #add col number
    for i in range(len(every_list)):
        if i == 0:
            continue
        every_list[i] = str(i)+":"+every_list[i]

    #remove col which value==0
    for j in every_list:
        if j.split(":")[-1] == '0':
        #if float(j.split(':')[-1]) == 0:
            every_list.remove(j)

    print every_list

结果是错的:

['5001', '2:0', '3:20100307', '4:20150109', '5:2', '6:3', '7:75', '9:0', '10:114', '11:13', '12:2', '14:0']
['10001', '2:2', '3:20100318', '4:20150119', '5:2', '6:3', '7:58', '9:0', '10:1', '11:5', '12:1', '14:0']
['15001', '2:2', '3:20100329', '4:20150130', '5:9', '6:14', '7:70', '9:0', '10:1', '11:31', '12:7', '13:11', '14:7']
['20001', '2:0', '3:20100401', '4:20150117', '5:3', '6:5', '7:71', '8:1', '9:75', '10:1', '11:1', '12:1', '14:0']
['22001', '2:1', '3:20100401', '4:20150125', '5:7', '6:11', '7:58', '9:0', '10:51', '11:40', '12:12', '13:2', '14:1']
['24001', '2:0', '3:20100401', '4:20141209', '5:1', '6:1', '7:138', '9:0', '10:1', '11:1', '12:1', '14:0']
['44001', '1:1', '3:20100428', '4:20141130', '5:1', '6:1', '7:85', '9:0', '10:1', '12:0', '14:0']
['45001', '1:1', '3:20100429', '4:20141127', '5:1', '6:1', '7:128', '9:0', '10:1', '11:24', '12:3', '14:0']
['50001', '2:0', '3:20100506', '4:20141230', '5:1', '6:2', '7:67', '9:0', '10:44', '11:6', '12:1', '13:4', '14:1']
['69001', '2:0', '3:20100524', '4:20150129', '5:16', '6:20', '7:90', '8:2', '9:220', '10:236', '11:44', '12:18', '13:3', '14:2']
['71001', '2:2', '3:20100526', '4:20141224', '5:9', '6:15', '7:64', '9:0', '10:57', '11:30', '12:7', '13:11', '14:4']

分析:

因为python list的时候删除元素,不好。

这就是不好的原因:

>>> s = [1,2,3,4,5]
>>> s
[1, 2, 3, 4, 5]
>>> for i in s:
	s.remove(i)
	print s

[2, 3, 4, 5]
[2, 4, 5]
[2, 4]
>>> 

删除不干净。

后来神来之笔,把list变成list[:]:

arrays = [
['5001', '0', '0', '20100307', '20150109', '2', '3', '75', '0', '0', '114', '13', '2', '0', '0'],
['10001', '0', '2', '20100318', '20150119', '2', '3', '58', '0', '0', '1', '5', '1', '0', '0'],
['15001', '0', '2', '20100329', '20150130', '9', '14', '70', '0', '0', '1', '31', '7', '11', '7'],
['20001', '0', '0', '20100401', '20150117', '3', '5', '71', '1', '75', '1', '1', '1', '0', '0'],
['22001', '0', '1', '20100401', '20150125', '7', '11', '58', '0', '0', '51', '40', '12', '2', '1'],
['24001', '0', '0', '20100401', '20141209', '1', '1', '138', '0', '0', '1', '1', '1', '0', '0'],
['44001', '1', '0', '20100428', '20141130', '1', '1', '85', '0', '0', '1', '0', '0', '0', '0'],
['45001', '1', '0', '20100429', '20141127', '1', '1', '128', '0', '0', '1', '24', '3', '0', '0'],
['50001', '0', '0', '20100506', '20141230', '1', '2', '67', '0', '0', '44', '6', '1', '4', '1'],
['69001', '0', '0', '20100524', '20150129', '16', '20', '90', '2', '220', '236', '44', '18', '3', '2'],
['71001', '0', '2', '20100526', '20141224', '9', '15', '64', '0', '0', '57', '30', '7', '11', '4']
]

for every_list in arrays:
    #add col number
    for i in range(len(every_list)):
        if i == 0:
            continue
        every_list[i] = str(i)+":"+every_list[i]

    #remove col which value==0
    for j in every_list[1:]:
        if j.split(":")[-1] == '0':
        #if float(j.split(':')[-1]) == 0:
            every_list.remove(j)

    print every_list

因为list[1:]产生一个新的副本,也就是避开了一边遍历,一边删除的尴尬,而是遍历副本,删除原对象。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-04 10:32:39

python list遍历时删除元素的相关文章

java 集合遍历时删除元素

本文探讨集合在遍历时删除其中元素的一些注意事项,代码如下 ? 1 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import java.util.ArrayList; import java.util.Iterator; import java

STL容器遍历时删除元素

STL容器遍历时在循环体内删除元素最容易出错了,根本原因都是因为迭代器有效性问题,在此记下通用删除方法,该方法适用于所有容器: 1 std::vector<int> myvec; 2 3 std::vector<int>::iterator it = myvec.begin(); 4 while( it != myvec.end()) 5 { 6 it = myvec.erase(it); 7 } 容器list有个比较另类的删除方法,如下代码所示: std::list<int

list遍历时删除元素

正如我们前面所知道. 在我们依靠遍历删除ArrayList时总是出现一些神奇的现象.比如 ArrayList<integer> arraylist = new ArrayList<integer>(); arraylist.add(1); arraylist.add(1); arraylist.add(2); arraylist.add(3); arraylist.add(1); 我们遍历并删除其中的1 for(int i = 0:i < arraylist.size();i

python中遍历list删除元素引发的问题与解决办法

引发问题的场景 今天在写一个小游戏的demo时,进行游戏元素操作时,遇到了一个问题.类似下面代码: list = ['a','b','c','d'] # element_type == list for i in list: print('元素的下标为{},元素的值{}'.format(list.index(i),list)) # 打出内容.方便查看 list.remove(i) print(list) 本意是遍历删除list中的所有元素.最后list应该为一个空数组. 但是代码实际运行结果是

多线程14-遍历集合时删除元素问题分析

1. 问题 创建一个User类: package cn.itcast.heima2; public class User implements Cloneable{ private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } public boolean equals(Object obj) { if(this == obj) { ret

Java HashMap 如何正确遍历并删除元素

(一)HashMap的遍历 HashMap的遍历主要有两种方式: 第一种采用的是foreach模式,适用于不需要修改HashMap内元素的遍历,只需要获取元素的键/值的情况. HashMap<K, V> myHashMap; for (Map.entry<K, V> item : myHashMap.entrySet()){ K key = item.getKey(); V val = item.getValue(); //todo with key and val //WARNI

Java集合遍历时删除

public static void main(String[] args){ List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); Iterator<Integer> interator = list.iterator(); while(interator.hasNext()){ Integer i

Java 遍历Map时 删除元素

1 package net.nie.test; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map; 6 7 public class HashMapTest { 8 private static Map<Integer, String> map=new HashMap<Integer,String>(); 9 10 /** 1.HashMap 类映射不保证顺序:某些映射

List的遍历和删除元素

/** * 遍历list的方法 * @param args */ public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("111"); list.add("222"); list.add("333"); list.add("222"); list.add("4