<h2>选择1</h2>
<p>这是一个更现代的解决方案,它只在<code>python3.x</code>中起作用,这与问题所采用的方法相同。您需要使用<code>t: (-t[1], t[0])</code>而不是<code>t[::-1]</code>作为索引。<code>-t[1]</code>按升序对名称排序,因为它已被忽略<code>t[0]</code>按降序对键进行排序。
<strong>代码如下:</strong></p>
<pre><code>dictionary = {"Sophie": 23, "Alfred": 23, "Zelda":22, "Betty":23}
reversed_dict= dict(sorted(dictionary.items(), key=lambda t: (-t[1], t[0])))
print(reversed_dict)
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>{'Alfred': 23, 'Betty': 23, 'Sophie': 23, 'Zelda': 22}
</code></pre>
<hr/>
<h2>选择2</h2>
<p>这是一个更具可移植性的解决方案,适用于所有版本的python。
<code>sorted(dictionary.items(), key=operator.itemgetter(0))</code>按升序排列名称,然后<code>sorted(reversed_dict, key=operator.itemgetter(1), reverse=True))</code>按降序排列键</p>
<p><strong>代码如下:</strong></p>
<pre><code>import operator
dictionary = {"Sophie": 23, "Alfred": 23, "Zelda":22, "Betty":23}
reversed_dict = sorted(dictionary.items(), key=operator.itemgetter(0))
reversed_dict = dict(sorted(reversed_dict, key=operator.itemgetter(1), reverse=True))
print(reversed_dict)
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>{'Alfred': 23, 'Betty': 23, 'Sophie': 23, 'Zelda': 22}
</code></pre>