从列表python的单个列表中删除子列表 [英] remove sublist from a single list of list python

查看:72
本文介绍了从列表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']]

我尝试使用以下代码:

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)]

输出:

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

这篇关于从列表python的单个列表中删除子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆