5
operator.itemgetter()和sort()如何工作?
我有以下代码: # 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]?可以吗 用运算符如何打印像3x2is这样的表格的某个值22? Python到底如何对表格进行排序?我可以对它进行反向排序吗? 我如何基于两列(例如第一个年龄,然后如果年龄是相同的b名称)对其进行排序? 没有我怎么办operator?