【发布时间】:2013-09-06 21:20:06
【问题描述】:
我有以下代码:
# initialize
a = []
# create the table (name, age, job)
a.append(["Nick", 30, "Doctor"])
a.append(["John", 8, "Student"])
a.append(["Paul", 22, "Car Dealer"])
a.append(["Mark", 66, "Retired"])
# sort the table by age
import operator
a.sort(key=operator.itemgetter(1))
# print the table
print(a)
它创建一个 4x3 表,然后按年龄对其进行排序。我的问题是,key=operator.itemgetter(1) 到底是做什么的? operator.itemgetter 函数是否返回项目的值?为什么我不能在那里输入key=a[x][1] 之类的东西?或者我可以吗?运算符如何打印表单的某个值,例如3x2,即22?
-
Python 究竟是如何对表格进行排序的?我可以对其进行反向排序吗?
-
如何根据第一年龄等两列对它进行排序,然后如果年龄是相同的 b 名称?
-
没有
operator怎么办?
【问题讨论】:
标签: python sorting operator-keyword