lets say I have a list:
A = ['x', 'y', 'z']
and another one - nested:
B = [['x', 'a', 'b', 'c'], ['y', 'c'], ['x', 'a', 'c', 'z']]
how can I remove 'z' from the last sublist in list B based on the knowledge that 'z' is in A and also every first element [0] of the sublist B is in list A ?
so basically i need to detele all elements from such a nested list where element is in A and where it not stands in the first position of that nested list ?
I am trying this but get stacked:
for i in B:
for j in i[1:]:
if j in A:
del j
but I am missing something.
Use a list comprehension to update a list in-place:
for sublist in B:
if sublist[0] in A:
sublist[1:] = [v for v in sublist[1:] if v not in A]
j
in your loop is only another reference to a value in a sublist. del j
removes that one reference, but the list still contains that reference. You can remove values from the list with del listobj[index]
or listobj.pop(value)
(watch for subtleties in what those mean), but deleting from a list while iterating over it will result in items being skipped if you are not careful.
By assigning to a slice, you replace those elements in the list itself, in-place.
Note that you probably want to make A
a set ; membership testing is far faster when using a set:
Aset = set(A)
for sublist in B:
if sublist[0] in Aset:
sublist[1:] = [v for v in sublist[1:] if v not in Aset]
Demo:
>>> A = ['x', 'y', 'z']
>>> B = [['x', 'a', 'b', 'c'], ['y', 'c'], ['x', 'a', 'c', 'z']]
>>> Aset = set(A)
>>> for sublist in B:
... if sublist[0] in Aset:
... sublist[1:] = [v for v in sublist[1:] if v not in Aset]
...
>>> B
[['x', 'a', 'b', 'c'], ['y', 'c'], ['x', 'a', 'c']]
You can use a nested list comprehension:
>>> [sub[:1]+[i for i in sub[1:] if not(sub[0] in A and i in A)] for sub in B]
[['x', 'a', 'b', 'c'], ['y', 'c'], ['x', 'a', 'c']]
Here you'll preserve the items from sublists (from second item to end) that doesn't met your conditions for deleting.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.