<p>我有一个字典,每个键都有一个项目列表(向量):</p>
<pre><code>from collections import defaultdict
dict = defaultdict(list)
dict[133] = [2,4,64,312]
dict[4] = [2,3,5,12,45,32]
dict[54] = [12,2,443,223]
def getTotalVectorItems(items):
total = 0
for v in items.values():
total += len(v)
return total
print getTotalVectorItems(dict)
</code></pre>
<p>这将打印:</p>
<pre><code>14 # number of items in the 3 dict keys.
</code></pre>
<p>除了创建这个“getTotalVectorItems”函数之外,还有更简单的python方法吗?我觉得有一个快速的方法可以做到这一点。你知道吗</p>