operator.itemgetter和sort()在Python中如何工作?

编程入门 行业动态 更新时间:2024-10-10 13:16:11
本文介绍了operator.itemgetter和sort()在Python中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码:

# 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?

  • Python如何对表进行精确排序?我可以对它进行反向排序吗?

  • 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怎么办?

    推荐答案

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

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

    operator是一个内置模块,提供了一组便捷的运算符. operator.itemgetter(n)用两个词构造一个可调用对象,该对象假定一个可迭代对象(例如list,tuple,set)作为输入,并从中获取第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.

    其他问题:

  • 是的,您可以反向排序,只需添加reverse=True:a.sort(key=..., reverse=True)
  • 要按多个列进行排序,可以将itemgetter与多个索引一起使用:operator.itemgetter(1,2),或者将lambda与lambda elem: (elem[1], elem[2])一起使用.这样,可迭代地为列表中的每个项目构造可迭代对象,然后按lexicographic(?)顺序将它们相互比较(比较第一个元素,如果相等,则比较第二个元素,等等)
  • 您可以使用a[2,1]在[3,2]处获取值(索引从零开始).使用运算符...可能,但是不像索引那样干净.
  • Yes, you can reverse sort, just add reverse=True: a.sort(key=..., reverse=True)
  • 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)
  • 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.
  • 有关详细信息,请参阅文档:

    Refer to the documentation for details:

  • operator.itemgetter解释
  • 通过Python中的自定义键排序列表
  • operator.itemgetter explained
  • Sorting list by custom key in Python
  • 更多推荐

    operator.itemgetter和sort()在Python中如何工作?

    本文发布于:2023-06-03 09:51:45,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/471223.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:工作   itemgetter   operator   Python   sort

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!