删除有空列表的列表(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]更多推荐
发布评论