I need to delete these lists inside of list that contains the /
symbol.
List for example:
X = [['a/','$1'], ["c","d"]]
so X[0]
should be deleted. The actual list are much longer and contains more instances of this condition.
I tried use something like:
print([l for l in X if l.count("/") <1])
But if I understand correctly because the /
is attached to another symbol he is not counted.
Should I convert this list of lists to string, separate the /
from another character, and then use the count function, or there is better solution?
CodePudding user response:
One way to search "/"
in each item in the sublists is to wrap a generator expression with any
. Since you don't want sublists with "/"
in it, the condition should be not any()
:
out = [lst for lst in X if not any('/' in x for x in lst)]
Output:
[['c', 'd']]
CodePudding user response:
The call to filter() applies that lambda function to every list in X and filters out list with '/'.
result = list(filter(lambda l: not any('/' in s for s in l), X))
CodePudding user response:
Given:
X = [['a/','$1'], ["c","d"]]
You can convert the sub lists to their repr
string representations and detect the /
in that string:
new_x=[sl for sl in X if not '/' in repr(sl)]
Or, you can use next
:
new_x=[sl for sl in X if not next('/' in s for s in sl)]
Either:
>>> new_x
[['c', 'd']]