Python - removing items

I want to remove an item from the list called mom. I have another list called cut

mom= [[0,8,1], [0, 6, 2, 7], [0, 11, 12, 3, 9], [0, 5, 4, 10]] cut =[0, 9, 8, 2] 

How to remove what is cut from mom except zero?

Result of my desire

 mom=[[0,1],[0,6,7],[0,11,12,3],[0,5,4,10]] 
+8
python genetic
source share
4 answers
 >>> [[e for e in l if e not in cut or e == 0] for l in mom] [[0, 1], [0, 6, 7], [0, 11, 12, 3], [0, 5, 4, 10]] 
+9
source share

Here is how I would do it with an understanding of List.

 mom= [[0,8,1], [0, 6, 2, 7], [0, 11, 12, 3, 9], [0, 5, 4, 10]] cut =[0, 9, 8, 2] mom = [[x for x in subList if x not in cut or x == 0 ] for subList in mom ] 
0
source share

The answers provided by Ingnacio and Dom are perfect. The same can be done in a more understandable and understandable way. Try the following:

mom = [[0,8,1]], [0, 6, 2, 7], [0, 11, 12, 3, 9], [0, 5, 4, 10]]

 cut =[0, 9, 8, 2] for e in mom: for f in e: if f in cut and f != 0: e.remove(f) #used the remove() function of list print(mom) 

Much easier for beginners in Python. Is not it?

0
source share

Given the cut = [0,9,8,2] and mom = [[0,8,1], [0, 6, 2, 7], [0, 11, 12, 3, 9], [0, 5 , 4, 10]]

Assuming item 0 is removed from the clipped list

open = [9,8,2]

result = [] for e in mom: result.append (list (set (e) -set (cut)))

o / r Result

[[0, 1], [0, 6, 7], [0, 11, 3, 12], [0, 10, 4, 5]]

0
source share

All Articles