我试图制作排行榜,而我组织它的方式是这样的:
leaderboard=['-7\tPlayerName','6\tPlayerName','2\tPlayerName','-11\tPlayerName']
问题在于,当我执行leaderboard.sort()
时,它并没有按照我想要的顺序排列它们。我知道可能有更好的方法来做到这一点。如果你能想到一个,请告诉我。但我更喜欢这样做。我的最终目标是在对其进行排序之后,它应该是这样的:
leaderboard=['-11\tPlayerName','-7\tPlayerName','2\tPlayerName','6\tPlayerName']
答案 0 :(得分:0)
In [260]: leaderboard=['-7\tPlayerName','6\tPlayerName','2\tPlayerName','-11\tPlayerName']
In [261]: sorted(leaderboard, key=lambda x: int(x.split('\t')[0]))
Out[261]: ['-11\tPlayerName', '-7\tPlayerName', '2\tPlayerName', '6\tPlayerName']
In [262]: leaderboard.sort(key=lambda x: int(x.split('\t')[0]))
KeyboardInterrupt
In [262]: leaderboard
Out[262]: ['-7\tPlayerName', '6\tPlayerName', '2\tPlayerName', '-11\tPlayerName']
In [263]: leaderboard.sort(key=lambda x: int(x.split('\t')[0]))
In [264]: leaderboard
Out[264]: ['-11\tPlayerName', '-7\tPlayerName', '2\tPlayerName', '6\tPlayerName']