python - 从列表 python 的单个列表中删除子列表
问题描述
我已经完成了Removing sublists from a list of lists,但是当我为我的数据集扩展它时,它不适用于我的情况。因此发布了一个新问题。
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']]
我想删除一些元素并在下面发布所需的输出:
**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)]
解决方案
您必须将子列表中的所有元素都拆分,,
才能使用您已经找到的解决方案:
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 - 使用 Cognito 凭证的 AWS IOT 连接
- html - 为什么一个元素在不同时间进行颜色转换?
- ios - 使用旧版 Xcode 发布到 App Store
- angular - 如何在 Angular 中增加默认超时
- jquery - CSS 'snap-scroll' 干扰 jQuery '.animate scrollLeft'
- visual-studio-code - 快速建议在片段占位符内不起作用
- java - Intellij 中的 JavaFX 和 Maven:设置了 JAVA_HOME 但“无法识别的选项 --module-path”错误仍然存在
- wpf - 如何将样式应用于组合框模板
- javascript - 如何在Javascript中找到与正则表达式匹配的属性?
- sql - Postgres 列必须出现在 GROUP BY 子句中或在聚合函数中使用