Python 3对元组列表进行排序?

Abi*_*ilB 3 python sorting list

我是Python新手,我有一个问题。有人告诉我要问这个问题,与我为同一程序编写的另一个帖子分开问。这是家庭作业,所以我只需要一些指导。我有一个元组列表,我想按tuple [0]对其进行排序,然后将要打印的完整元组返回到屏幕。元组由(得分,标记(x或o),索引)组成

这是我的基本代码(井字游戏的一部分-我在另一篇文章中有完整代码):::

listOfScores = miniMax(gameBoard)
best = max(listOfScores, key=lambda x: x[0])

if best[0] == 0:
            print("You should mark " + best[1] + " in cell " + best[2] + ".")
            print("This will lead to a tie.")
        elif best[0] > 0:
            print("You should mark " + best[1] + " in cell " + best[2] + ".")
            print("This will lead to a win.")
        else:
            print("You should mark " + best[1] + " in cell " + best[2] + ".")
            print("This will lead to a loss.")
Run Code Online (Sandbox Code Playgroud)

我收到此错误:::

Traceback (most recent call last):
  File "C:\Users\Abby\Desktop\CS 3610\hw2\hw2Copy.py", line 134, in <module>
    main()
  File "C:\Users\Abby\Desktop\CS 3610\hw2\hw2Copy.py", line 120, in main
    best = max(listOfScores, key=lambda x: x[0])
TypeError: unorderable types: list() > int()
Run Code Online (Sandbox Code Playgroud)

我不确定为什么会这样。这是我第一次尝试使用此功能:

best = max(listOfScores, key=lambda x: x[0])
Run Code Online (Sandbox Code Playgroud)

所以我认为我可能使用不正确。是否有更好的方式对这些元组进行排序(从最大到最小,从最小到最大),以便我可以检索到最小或最大值?谢谢!:)

Ter*_*ryA 6

如果要排序,请使用sorted();)

best = sorted(listOfScores, key=lambda x: x[0])
Run Code Online (Sandbox Code Playgroud)

这会将其从最低得分到最高得分进行排序。