<p>我有点被家庭作业问题困住了。
假设我有一个这样的列表:</p>
<p><code>[('Paul George', 1), ('Luke Skywalker', 2), ('Mitchell Piker', 3), ('Phil Dam', 1)]</code></p>
<p>我必须编写一个函数,使列表按第二个元素(即整数值)排序。另外,如果两个人有相同的整数,那么我必须按字母顺序排序。另外,我可以使用<code>sort</code>,但不能使用可选参数<code>key</code>。所以我应该有这样的东西:</p>
<p><code>[('Mitchell Piker', 3), ('Luke Skywalker', 2), ('Paul George', 1), ('Phil Dam', 1)]</code></p>
<pre><code>>>> def sort(lst: List[Tuple[str, int]]):
"""Return the list of tuples from largest integer score to lowest
integer score. If two people have the same score, then sort them by
alphabetical order.
"""
lst2 = []
lst2.append(lst[0])
for i in range(len(lst)):
for j in range(len(lst2)):
if lst[i][-1] > lst2[j][-1] and lst[i][-1] not in lst2:
lst2.insert(0, lst2[i])
if lst[i][-1] < lst2[j][-1] and lst[i][-1] not in lst2:
lst2.append(lst2[i][-1])
return lst2
</code></pre>
<p>但是我得到一个错误。有人能帮忙吗?非常感谢</p>