operator.itemgetter() 和 sort() 如何工作? [英] How do operator.itemgetter() and sort() work?

查看:66
本文介绍了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] 的东西?或者我可以吗?运算符如何打印3x2这样的形式的某个值,即22?

It creates a 4x3 table and then it sorts it by age. My question is, what exactly key=operator.itemgetter(1) does? Does the operator.itemgetter function return the item's value? Why can't I just type something like key=a[x][1] there? Or can I? How could with operator print a certain value of the form like 3x2 which is 22?

  1. Python 究竟是如何对表格进行排序的?我可以反向排序吗?

  1. How does exactly Python sort the table? Can I reverse-sort it?

如何根据两列对它进行排序,比如第一个年龄,然后如果年龄是相同的 b 名称?

How can I sort it based on two columns like first age, and then if age is the same b name?

如果没有 operator,我怎么办?

How could I do it without operator?

推荐答案

看起来你对所有这些东西有点困惑.

Looks like you're a little bit confused about all that stuff.

operator 是一个内置模块,提供了一组方便的操作符.用两个词 operator.itemgetter(n) 构造一个可调用对象,它假定一个可迭代对象(例如列表、元组、集合)作为输入,并从中取出第 n 个元素.

operator is a built-in module providing a set of convenient operators. In two words operator.itemgetter(n) constructs a callable that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it.

所以,你不能在那里使用 key=a[x][1],因为 python 不知道 x 是什么.相反,您可以使用 lambda 函数(elem 只是一个变量名,没有魔法):

So, you can't use key=a[x][1] there, because python has no idea what x is. Instead, you could use a lambda function (elem is just a variable name, no magic there):

a.sort(key=lambda elem: elem[1])

或者只是一个普通的函数:

Or just an ordinary function:

def get_second_elem(iterable):
    return iterable[1]

a.sort(key=get_second_elem)

所以,这里有一个重要的注意事项:在 Python 中,函数是 一等公民,所以你可以将它们作为参数传递给其他函数.

So, here's an important note: in python functions are first-class citizens, so you can pass them to other functions as a parameter.

其他问题:

  1. 是的,你可以反向排序,只需添加reverse=True:a.sort(key=..., reverse=True)
  2. 要按多列排序,您可以使用带有多个索引的 itemgetter:operator.itemgetter(1,2),或使用 lambda:lambda elem: (elem[1], elem[2]).通过这种方式,可以为列表中的每个项目动态构建可迭代对象,然后按字典(?)顺序相互比较(比较第一个元素,如果相等 - 比较第二个元素等)
  3. 您可以使用 a[2,1] 获取 [3,2] 处的值(索引从零开始).使用运算符...这是可能的,但不像索引那样干净.
  1. Yes, you can reverse sort, just add reverse=True: a.sort(key=..., reverse=True)
  2. To sort by more than one column you can use itemgetter with multiple indices: operator.itemgetter(1,2), or with lambda: lambda elem: (elem[1], elem[2]). This way, iterables are constructed on the fly for each item in list, which are than compared against each other in lexicographic(?) order (first elements compared, if equal - second elements compared, etc)
  3. You can fetch value at [3,2] using a[2,1] (indices are zero-based). Using operator... It's possible, but not as clean as just indexing.

详情请参考文档:

  1. operator.itemgetter 解释
  2. 在 Python 中按自定义键排序列表

这篇关于operator.itemgetter() 和 sort() 如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆