Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python- Removing items

Tags:

python

genetic

I want to remove item from a 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 do I remove what in cut from mom, except for zero?

My desire result is

mom=[[0,1],[0,6,7],[0,11,12,3],[0,5,4,10]]
like image 422
user02 Avatar asked Apr 23 '17 03:04

user02


People also ask

How do you delete specific items in Python?

The remove() method removes the first matching element (which is passed as an argument) from the list. The pop() method removes an element at a given index, and will also return the removed item. You can also use the del keyword in Python to remove an element or slice from a list.

How do I remove an item from a string in Python?

Python Remove Character from String using replace() If we provide an empty string as the second argument, then the character will get removed from the string. Note that the string is immutable in Python, so this function will return a new string and the original string will remain unchanged.

What does Delete () do in Python?

Definition and Usage. The del keyword is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list etc.

How do you remove three items from a list in Python?

In Python, use list methods clear() , pop() , and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.


1 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]]
like image 95
Ignacio Vazquez-Abrams Avatar answered Sep 30 '22 18:09

Ignacio Vazquez-Abrams