我想删除具有空值的字典元素列表。
所以对于以下内容:
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)
答案 0 :(得分:5)
试试这个:
print([d for d in arr if all(d.values())])
不要使用dict
作为名称:覆盖Python的名称。在你的情况下,它不是字典,而是一系列字典。