python - 创建表并从字典中的列表中排序数据

标签 python python-3.x list sorting dictionary

我有一本包含列表的字典

    students = {
1:["name1", 20, 100],
2:["name2", 20, 100],
3:["name3", 20, 95],
4:["name4", 21, 65],
5:["name5", 22, 85]
}

我想创建表格并按名称对数据进行排序。像这样

Name        Age       Score
name1       20        100
name2       20        100
name3       20        95
name4       21        65
name5       22        85

我该怎么做? 我是这样写的:

students = {
1:["Rauf", 20, 100],
2:["Shafag", 20, 100],
3:["Ali", 20, 95],
4:["Camal", 21, 65],
5:["Arzu", 22, 85]

}
print(" ")
print("{:>12} {:>12} {:>12}".format('Name','Age','Score'))
print(" ")
for v in students.values():
    name, age, score = v
    print(sorted(("{:>12} {:>12} {:>12}".format(name, age, score)), key= lambda v: v[2]))

最佳答案

这有帮助吗

import operator
students = {
1:["Rauf", 20, 100],
2:["Shafag", 20, 100],
3:["Ali", 20, 95],
4:["Camal", 21, 65],
5:["Arzu", 22, 85]

}
print(" ")
print("{:>12} {:>12} {:>12}".format('Name','Age','Score'))
sorted_students = sorted(students.items(), key=operator.itemgetter(1))
print(" ")
for v in sorted_students:
    name, age, score = v[1]
    print("{:>12} {:>12} {:>12}".format(name, age, score))

关于python - 创建表并从字典中的列表中排序数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51204963/

相关文章:

python - 找不到记录器 "elasticsearch.trace"的处理程序

python - 如何使用 Python 检查 Celery/Supervisor 是否正在运行

python - 与特殊值交织的排列

python-3.x - 使用 py.test 在 python 中测试和调试数据存储代码的问题

python - 遍历文件从列表中查找单词时的 findall() 正则表达式

python - SQLAlachmey : ORM filter to match all items in a list, 没有

python - 使用 pyodbc 从数据库中提取 SQL 脚本

python - 从列表中删除整个列表

Java 包含不能按预期工作,因为 "someString"!= "someString"

python - 在python中拆分字符串以获得一个值?