Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
消除由列表和数组组成的python dict中的空值_Python_Arrays_List_Dictionary - Fatal编程技术网

消除由列表和数组组成的python dict中的空值

消除由列表和数组组成的python dict中的空值,python,arrays,list,dictionary,Python,Arrays,List,Dictionary,我想消除dict中的所有空值,dict的值是列表和nd数组的混合。所以我试着: res = [ele for ele in ({key: val for key, val in sub.items() if val} for sub in test_list) if ele] 但是我得到了错误 ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a

我想消除dict中的所有空值,dict的值是列表和nd数组的混合。所以我试着:

    res = [ele for ele in ({key: val for key, val in sub.items() if val} for sub in test_list) if ele]
但是我得到了错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). And if I try:
AttributeError: 'list' object has no attribute 'any'

我得到了错误

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(). And if I try:
AttributeError: 'list' object has no attribute 'any'

所以我想知道在python中是否有更通用的方法来删除空值
dict

检查空列表的常用方法是检查
len(list)
。所以假设您的
dict()
看起来是这样的

myDict = {
  1: [1,2,3],
  2: [],
  3: np.array([[1,2],[3,4],[5,6]])
}
您的列表可能如下所示

res = {k:v for k,v in myDict.items() if len(v)}

注意听写理解中的
len(v)

我认为你把这一步做得过于复杂了(而且没有包括一个完整的例子!)

下面的示例创建一个新的dict
res
,其中包含所有非空值的
test\u dict
。我在这里使用了
len()
,因为这对列表和nd数组都有效。对于just list,我将省略对
len()
的调用,而只使用
val

test_dict = {1: [], 2: [1,2,3], 3: [4,5,6]}
res = {key: val for key, val in test_list.items() if len(val)}
如果您想使用any(),您将查找包含至少一个truthy项的列表的dict值:

test_dict = {1: [], 2: [1,2,3], 3: [4,5,6]}
res = {key: val for key, val in test_list.items() if any(val)}

@雅各布的回答很好,但实际上效率很低

相反,您可以利用内置的
filter()
方法过滤掉空字典,并使用
dict()
方法而不是使用
dict
理解:

res = filter(None, (dict(i for i in sub.items() if len(i[1])) for sub in test_list))

请修复帖子,以显示使用
any
;您第二次复制粘贴了错误消息。但更重要的是:在编写
{key:val for key,val in sub.items()if val}
的地方,您对
if val
的实际规则是什么?对于普通列表,一个空列表将失败,并且其他所有可能的列表都将通过;这就是你的意图吗?对于Numpy数组,哪个应该通过,哪个应该失败?当您应该执行
any(array)
时,您正在调用
array.any()
,这就是为什么会出现第二个错误
any
是内置函数,不是一般python数组的属性。(修复此问题可能不会修复您的函数,但它是导致错误的原因)