gpt4 book ai didi

python - 删除具有空列表的列表

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:06 25 4
gpt4 key购买 nike

我有以下列表:

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)

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

最佳答案

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

关于python - 删除具有空列表的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35223618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com