删除有空列表的列表(Removing list having empty list)

系统教程 行业动态 更新时间:2024-06-14 16:59:17
删除有空列表的列表(Removing list having empty list)

我有以下列表:

a = [[['trial1', 'trial2'], 4], [[], 2]]

我想删除列表中有一个空列表。所以下面的结果:

c = [[['trial1', 'trial2'], 4]]

我正在使用下面的代码:

c = [] for b in a: temp =[x for x in b if x] if len(temp)>1: c.append(temp)

它工作正常,但它似乎不是一个完成此任务的“好方法”。 有没有更优雅的方式?

I have the following list:

a = [[['trial1', 'trial2'], 4], [[], 2]]

and I want to remove the list that have inside an empty list.So the following result:

c = [[['trial1', 'trial2'], 4]]

I am using the following code:

c = [] for b in a: temp =[x for x in b if x] if len(temp)>1: c.append(temp)

It works ok, but it seems not to be a 'good way' of doing this task. Is there a more elegant way?

最满意答案

c = [l for l in a if [] not in l] c = [l for l in a if [] not in l]

更多推荐

本文发布于:2023-04-16 14:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/3993482759bfe581d0da21d8e425d1ec.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   Removing   empty   list

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!