Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将嵌套列表字符串转换为整数,然后在Python3中对其进行排序?_Python_Sorting_Python 3.x_Nested Lists - Fatal编程技术网

如何将嵌套列表字符串转换为整数,然后在Python3中对其进行排序?

如何将嵌套列表字符串转换为整数,然后在Python3中对其进行排序?,python,sorting,python-3.x,nested-lists,Python,Sorting,Python 3.x,Nested Lists,不是一个有经验的程序员!目前正在学校学习计算机GCSE,需要帮助解决一个问题 我有一个嵌套列表,其中保存了学生姓名的信息,然后是文本文件中的分数,这个文件需要导入到嵌套列表中。我已经使用代码完成了这项工作- scoresave = [] with open('class1quizscoreboard.txt') as scoreboard: for line in scoreboard: scoresave.append(line.strip().split(','))

不是一个有经验的程序员!目前正在学校学习计算机GCSE,需要帮助解决一个问题

我有一个嵌套列表,其中保存了学生姓名的信息,然后是文本文件中的分数,这个文件需要导入到嵌套列表中。我已经使用代码完成了这项工作-

scoresave = []
with open('class1quizscoreboard.txt') as scoreboard:
    for line in scoreboard:
        scoresave.append(line.strip().split(','))
print (scoresave)
这与

[['Emily Scott', ' 7'], ['Student Name', ' 6'], ['Another Student', ' 2']]
这是我所期望的,但是我如何将学生的分数转换成整数呢

我已尝试过此网站上的多种类似解决方案,但没有一种对我有效。

您可以使用:

sorted(map(lambda x: [x[0], int(x[1])], scoresave), key=lambda x: x[1])
得到你想要的

说明:

这会将嵌套列表的第二个元素转换为整数

map(lambda x:[x[0],int(x[1]),scoresave)

我们将整个过程分为以下几部分:

sorted({},key=lambda x:x[1])

“我尝试了多种解决方案”——您能分享一下您最近的尝试吗?“没有人为我工作过”——更具体一点;提供错误回溯或输入以及预期和实际输出。@GeorgiaSteele
排序(map(lambda x:[x[0],int(x[1]),scoresave),key=lambda x:x[0])
唯一更改的是索引
x[1]
->
x[0]
。前面我们是按第二个元素(整数x[1])排序的。要按字母顺序排序,请对第一个元素x[0]进行排序