擅长:python、mysql、java
<p>除了Miket25的答案之外,您还可以使用数字作为字典键,例如:</p>
<pre><code>d = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0 }
for i in s:
# 0 <= i < 5 is the same as looking through and checking
# all values 0-4 but more efficient and cleaner.
if 0 <= i < 5:
d[i] += 1
</code></pre>