Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove element if it has a certain string in it

Tags:

python

list

I have a list ['1 2 4 5 0.9', '1 2 4 5 0.6', '1 2 4 5 0.3', '1 2 4 5 0.4']

I also have another list: [0.9, 0.3, 0.7, 0.8]

I want to use the second list and the first list elements include whats in the second list then the element gets removed, so the first list ends up like:

[1 2 4 5 0.6', '1 2 4 5 0.4']
like image 485
John Smith Avatar asked Feb 17 '23 18:02

John Smith


1 Answers

You mean something like this:

>>> lst = ['1 2 4 5 0.9','1 2 4 5 0.6','1 2 4 5 0.3','1 2 4 5 0.4']
>>> s = set([0.9,0.3,0.7,0.8])
>>> [x for x in lst if float(x.split()[-1]) not in s]
['1 2 4 5 0.6', '1 2 4 5 0.4']
like image 117
mgilson Avatar answered Feb 19 '23 09:02

mgilson