擅长:python、mysql、java
<p>这样的东西在标准库中是不存在的。您可以使用<a href="http://docs.python.org/3.1/library/collections.html#collections.defaultdict" rel="noreferrer">^{<cd1>}</a>尽管:</p>
<pre><code>>>> from collections import defaultdict
>>> md = defaultdict(list)
>>> md[1].append('a')
>>> md[1].append('b')
>>> md[2].append('c')
>>> md[1]
['a', 'b']
>>> md[2]
['c']
</code></pre>
<p>(您可能希望使用<code>set</code>,而不是<code>list</code>,在这种情况下,您将调用<code>.add</code>,而不是<code>.append</code>。)</p>
<hr/>
<p><strong>作为旁白:看看你写的这两行:</p>
<pre><code>a[1] = 'a'
a[1] = 'b'
</code></pre>
<p>这似乎表明您希望表达式<code>a[1]</code>等于两个不同的值。这在字典中是不可能的,因为它们的键是唯一的,并且每个键都与一个值相关联。但是,<em>可以</em>做的是逐个提取与给定键相关联的列表中的所有值。您可以使用<a href="http://docs.python.org/3.1/library/functions.html#iter" rel="noreferrer">^{<cd7>}</a>和对<a href="http://docs.python.org/3.1/library/functions.html#next" rel="noreferrer">^{<cd8>}</a>的连续调用。也可以只使用两个循环:</p>
<pre><code>>>> for k, v in md.items():
... for w in v:
... print("md[%d] = '%s'" % (k, w))
...
md[1] = 'a'
md[1] = 'b'
md[2] = 'c'
</code></pre>