<p>我发现自己需要遍历一个字典列表,而且每次迭代都需要我正在迭代的字典的名称。</p>
<p>这是一个MWE(与本例无关):</p>
<pre><code>dict1 = {...}
dicta = {...}
dict666 = {...}
dict_list = [dict1, dicta, dict666]
for dc in dict_list:
# Insert command that should replace ???
print 'The name of the dictionary is: ', ???
</code></pre>
<p>如果我只使用<code>dc</code>其中<code>???</code>,它将打印字典的全部内容。我怎样才能知道正在使用的词典的名字?</p>
<p>您还应该考虑在每个字典中添加一个“name”键。</p>
<p>名字应该是:</p>
<pre><code>for dc in dict_list:
# Insert command that should replace ???
print 'The name of the dictionary is: ', dc['name']
</code></pre>