Python list pass-through delete element

Source: Internet
Author: User
Tags python list

Python list traversal time to delete really need to pay attention to, today to help the classmate to deal with data, unexpectedly silly force.

Demand:

In addition to the first column, add an ordinal to each column such as "1:0", "2:0", "3:20,100,307", and then delete the data after the colon as 0.


Recommended Practice:

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 '  , ' 1 '],[' 24001 ', ' 0 ', ' 0 ', ' 20100401 ', ' 20141209 ', ' 1 ', ' 1 ', ' 138 ', ' 0 ', ' 0 ', ' 1 ', ' 1 ', ' 1 ', ' 0 ', ' 0 '],[' 44001 ', ' 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 ', ' + ', ' 0 ', ' 0 ', ' arrays ', ' 4 ', ' 7 ', ' One ', ' Every_list ']]for: #add col num ber for I in range (len (every_list)): if i = = 0:continue Every_list[i] = str (i) + ":" +every_lis T[i] #remove col which value==0 every_list = filter (lambda e:e.split (":") [-1]! = ' 0 ', every_list) print every _list
Results:

[' 5001 ', ' 3:20,100,307 ', ' 4:20,150,109 ', ' 5:2 ', ' 6:3 ', ' 7:75 ', ' 10:114 ', ' 11:13 ', ' 12:2 '] [' 10001 ', ' 2:2 ', ' 3:20,100,318 ', ' 4:20,150,119 ', ' 5:2 ', ' 6:3 ', ' 7:58 ', ' 10:1 ', ' 11:5 ', ' 12:1 '] [' 15001 ', ' 2:2 ', ' 3:20,100,329 ', ' 4:20,150,130 ', ' 5:9 ', ' 6:14 ', ' 7:70 ', ' 10:1 ', ' 11:31 ', ' 12:7 ', ' 13:11 ', ' 14:7 '] [' 20001 ', ' 3:20,100,401 ', ' 4:20,150,117 ', ' 5:3 ', ' 6:5 ', ' 7:71 ', ' 8:1 ', ' 9:75 ', ' 10:1 ', ' 11:1 ', ' 12:1 '] [' 22001 ', ' 2:1 ', ' 3:20,100,401 ', ' 4:20,150,125 ', ' 5:7 ', ' 6:11 ', ' 7:58 ', ' 10:51 ', ' 11:40 ', ' 12:12 ', ' 13:2 ', ' 14:1 '] [' 24001 ', ' 3:20,100,401 ', ' 4:20,141,209 ', ' 5:1 ', ' 6:1 ', ' 7:138 ', ' 10:1 ', ' 11:1 ', ' 12:1 '] [' 44001 ', ' 1:1 ', ' 3:20,100,428 ', ' 4:20,141,130 ', ' 5:1 ', ' 6:1 ', ' 7:85 ', ' 10:1 '] [' 45001 ', ' 1:1 ', ' 3:20,100,429 ', ' 4:20,141,127 ', ' 5:1 ', ' 6:1 ', ' 7:128 ', ' 10:1 ', ' 11:24 ', ' 12:3 '] [' 50001 ', ' 3:20,100,506 ', ' 4:20,141,230 ', ' 5:1 ', ' 6:2 ', ' 7:67 ', ' 10:44 ', ' 11:6 ', ' 12:1 ', ' 13:4 ', ' 14:1 '] [' 69001 ', ' 3:20,100,524 ', ' 4:20,150,129 ', ' 5:16 ', ' 6:20 ', ' 7:90 ', ' 8:2 ', ' 9:220 ', ' 10:236 ', ' 11:44 ', ' 12:18 ', ' 13:3 ', ' 14:2 ' [ ' 71001 ', ' 2:2 ', ' 3:20,100,526 ', ' 4:20141224 ', ' 5:9 ', ' 6:15 ', ' 7:64 ', ' 10:57 ', ' 11:30 ', ' 12:7 ', ' 13:11 ', ' 14:4 '] 



In fact, the beginning of the silly practice:

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 '  , ' 1 '],[' 24001 ', ' 0 ', ' 0 ', ' 20100401 ', ' 20141209 ', ' 1 ', ' 1 ', ' 138 ', ' 0 ', ' 0 ', ' 1 ', ' 1 ', ' 1 ', ' 0 ', ' 0 '],[' 44001 ', ' 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 ', ' + ', ' 0 ', ' 0 ', ' arrays ', ' 4 ', ' 7 ', ' One ', ' Every_list ']]for: #add col num ber for I in range (len (every_list)): if i = = 0:continue Every_list[i] = str (i) + ":" +every_lis T[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
The result is wrong:

[' 5001 ', ' 2:0 ', ' 3:20,100,307 ', ' 4:20,150,109 ', ' 5:2 ', ' 6:3 ', ' 7:75 ', ' 9:0 ', ' 10:114 ', ' 11:13 ', ' 12:2 ', ' 14:0 '] [' 10001 ', ' 2:2 ', ' 3:20,100,318 ', ' 4:20,150,119 ', ' 5:2 ', ' 6:3 ', ' 7:58 ', ' 9:0 ', ' 10:1 ', ' 11:5 ', ' 12:1 ', ' 14:0 '] [' 15001 ', ' 2:2 ', ' 3:20,100,329 ', ' 4:20,150,130 ', ' 5:9 ', ' 6:14 ', ' 7:70 ', ' 9:0 ', ' 10:1 ', ' 11:31 ', ' 12:7 ', ' 13:11 ', ' 14:7 '] [' 20001 ', ' 2:0 ', ' 3:20,100,401 ', ' 4:20,150,117 ', ' 5:3 ', ' 6:5 ', ' 7:71 ', ' 8:1 ', ' 9:75 ', ' 10:1 ', ' 11:1 ', ' 12:1 ', ' 14:0 '] [' 22001 ', ' 2:1 ', ' 3:20,100,401 ', ' 4:20,150,125 ', ' 5:7 ', ' 6:11 ', ' 7:58 ', ' 9:0 ', ' 10:51 ', ' 11:40 ', ' 12:12 ', ' 13:2 ', ' 14:1 '] [' 24001 ', ' 2:0 ', ' 3:20,100,401 ', ' 4:20,141,209 ', ' 5:1 ', ' 6:1 ', ' 7:138 ', ' 9:0 ', ' 10:1 ', ' 11:1 ', ' 12:1 ', ' 14:0 '] [' 44001 ', ' 1:1 ', ' 3:20,100,428 ', ' 4:20,141,130 ', ' 5:1 ', ' 6:1 ', ' 7:85 ', ' 9:0 ', ' 10:1 ', ' 12:0 ', ' 14:0 '] [' 45001 ', ' 1:1 ', ' 3:20,100,429 ', ' 4:20,141,127 ', ' 5:1 ', ' 6:1 ', ' 7:128 ', ' 9:0 ', ' 10:1 ', ' 11:24 ', ' 12:3 ', ' 14:0 '] [' 50001 ', ' 2:0 ', ' 3:20,100,506 ', ' 4:20,141,230 ', ' 5:1 ', ' 6:2 ', ' 7:67 ', ' 9:0 ', ' 10:44 ', ' 11:6 ', ' 12:1 ', ' 13:4 ', ' 14:1 '] [' 69001 ', ' 2:0 ', '3:20,100,524 ', ' 4:20,150,129 ', ' 5:16 ', ' 6:20 ', ' 7:90 ', ' 8:2 ', ' 9:220 ', ' 10:236 ', ' 11:44 ', ' 12:18 ', ' 13:3 ', ' 14:2 ' [' 71001 ', ' 2 : 2 ', ' 3:20,100,526 ', ' 4:20,141,224 ', ' 5:9 ', ' 6:15 ', ' 7:64 ', ' 9:0 ', ' 10:57 ', ' 11:30 ', ' 12:7 ', ' 13:11 ', ' 14:4 ']
Analysis:

Because the Python list removes the element, it's not good.

This is a bad reason:

Delete is not clean.


Later genius, the list into 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 '  , ' 1 '],[' 24001 ', ' 0 ', ' 0 ', ' 20100401 ', ' 20141209 ', ' 1 ', ' 1 ', ' 138 ', ' 0 ', ' 0 ', ' 1 ', ' 1 ', ' 1 ', ' 0 ', ' 0 '],[' 44001 ', ' 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 ', ' + ', ' 0 ', ' 0 ', ' arrays ', ' 4 ', ' 7 ', ' One ', ' Every_list ']]for: #add col num ber for I in range (len (every_list)): if i = = 0:continue Every_list[i] = str (i) + ":" +every_lis T[i] #remove col which value==0 for J in Every_list[1:]: If J.split (":") [-1] = = ' 0 ': #if float (j.spli T (': ') [-1]) = = 0:every_list.remove (j) Print Every_list

Because List[1:] produces a new copy, that is, to avoid the side traversal, while deleting the embarrassment, instead of traversing the copy, delete the original object.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python list pass-through delete element

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.