简体   繁体   中英

remove sublist from a single list of list python

I have gone through Removing sublists from a list of lists , but it didn't work for my case when I extend it for my dataset. Hence posting a new question.

list1=[['A,C,D', 'Y', 'hello'],
['A,B,D', 'Y', 'hello'],
['B,C,D', 'Y', 'hello'],
['A,B,C,D', 'Y', 'hello'],
['A', 'Z', 'hello'],
['A,C', 'Z', 'hello'],
['B,C', 'Z', 'hello'],
['A,C', 'Z', 'hello'],
['A,B,C', 'Z', 'hello'],
['H,I,J,K', 'Z', 'hello'],
['H,K', 'Z', 'hello'],
['H,L', 'Z', 'hello'],
['I,J,K,L', 'Z', 'hello'],
['H,I,J,K,L', 'Z', 'hello'],
['B,C,D','Z','hi'],
['A,D,C,B','Z','hi']]

I want to remove few elements and posting the desired output below:

**Output**
[['A,B,C,D', 'Y', 'hello'],
 ['A,B,C', 'Z', 'hello'],
 ['H,I,J,K,L', 'Z', 'hello'],
 ['A,D,C,B','Z','hi']]

I have tried with the code below:

sets = [set(l) for l in lists]
new_list = [l for l,s in zip(lists, sets) if not any(s < other for other in sets)]

you have to split all by , al the elements from your sublists than you can use the solution you already have found:

st = [{i for e in l for i in e.split(',') } for l in list1]
[l for l, s in zip(list1, st) if not any(s < other for other in st)]

output:

[['A,B,C,D', 'Y', 'hello'],
 ['A,B,C', 'Z', 'hello'],
 ['H,I,J,K,L', 'Z', 'hello'],
 ['A,D,C,B', 'Z', 'hi']]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM