gpt4 book ai didi

Python - 从字典列表中删除具有空值的字典

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:17 25 4
gpt4 key购买 nike

我想删除具有空值的字典元素列表。

所以对于以下内容:

dict = [{"end": "00:15", "item": "Paul Test 1", "start": "00:00"}, {"end": "00:14", "item": "Paul Test 2 ", "start": "00:30"}, {"end": "", "item": "", "start": ""},  {"end": "", "item": "", "start": ""}, {"end": "", "item": "", "start": ""}]

将返回一个新的字典:

[{"end": "00:15", "item": "Paul Test 1", "start": "00:00"}, {"end": "00:14", "item": "Paul Test 2 ", "start": "00:30"}]

我试过:

{k:v for k,v in dict if not v}

但我得到:

ValueError: too many values to unpack (expected 2)

最佳答案

试试这个:

print([d for d in arr if all(d.values())])

不要使用 dict 作为名称:您会覆盖 Python 的名称。在您的情况下,它不是字典,而是字典数组。

关于Python - 从字典列表中删除具有空值的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46037007/

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