擅长:python、mysql、java
<p>我认为您使这一步变得过于复杂(并且没有包含完整的示例!)</p>
<p>下面的示例创建一个新的dict <code>res</code>,其中<code>test_dict</code>的所有值都具有非空值。我在这里使用了<code>len()</code>,因为这对列表和nd数组都有效。对于just list,我将省略对<code>len()</code>的调用,而只使用<code>val</code></p>
<pre><code>test_dict = {1: [], 2: [1,2,3], 3: [4,5,6]}
res = {key: val for key, val in test_list.items() if len(val)}
</code></pre>
<p>如果您想使用any(),您将查找包含至少一个truthy项的列表的dict值:</p>
<pre><code>test_dict = {1: [], 2: [1,2,3], 3: [4,5,6]}
res = {key: val for key, val in test_list.items() if any(val)}
</code></pre>