擅长:python、mysql、java
<p>您可以使用<code>defaultdict</code>:</p>
<pre><code>from collections import defaultdict
# initialize the result as a defaultdict with default value of 0
result = defaultdict(lambda : 0)
for t1,t2,s1,s2 in L:
if int(s1) > int(s2):
result[t1] += 1
elif int(s2) > int(s1):
result[t2] += 1
result
# defaultdict(<function __main__.<lambda>>, {'Patriots': 2, 'Steelers': 1})
</code></pre>
<p>请注意,即使在结果中,得分为零的团队丢失了,但是如果调用<code>result[team]</code>,它将为您提供零。你知道吗</p>