简体   繁体   English

Python-将函数分配给变量

[英]Python - assigning functions to variables

I have a list of tuples 我有一个元组列表

student_tuples = [
    ('john', 'A', 15),
    ('jane', 'B', 12),
    ('dave', 'B', 10),
]

I've been trying different ways to sort this, using itemgetter and lambda functions. 我一直在尝试使用itemgetter和lambda函数对这种方式进行排序。 Sorting by two indices of the tuples can be done with itemgetting and the lambda function, but it must return a tuple. 可以通过itemgetting和lambda函数完成对两个元组索引的排序,但是它必须返回一个元组。 I can't seem to find that anywhere in the documentation that the key function works on tuples. 我似乎在文档中的任何地方都找不到该键函数可用于元组的内容。

Anyway, I wanted to know what itemgetter() actually returns, so this works (copied from the itemgetter documentation): 无论如何,我想知道itemgetter()实际返回了什么,所以这可行(从itemgetter文档中复制):

f = itemgetter(1)
print f(student_tuples[0])
----->A

Is there any way to do this WITHOUT having to reassign itemgetter to a variable? 有什么方法可以做到,而不必将itemgetter重新分配给变量? It looks like two arguments are being passed, but something like 似乎正在传递两个参数,但是类似

print itemgetter(1, student_tuples[0])
-----><operator.itemgetter object at 0xf7309c8c>

doesn't give me anything useful. 没有给我任何有用的东西。

I'm just fiddling around trying to learn Python and this is confusing me. 我只是在摆弄尝试学习Python的事情,这使我感到困惑。 I don't know where in itemgetter student_tuples[0] is being added as an argument. 我不知道在itemgetter中的何处Student_tuples [0]被添加为参数。

The return value of itemgetter(1) is a function (actually, a callable object, but it's used like a function). itemgetter(1)的返回值是一个函数(实际上是一个可调用的对象,但它的使用类似于一个函数)。

The function it returns is roughly equivalent to the function that results from the expression: 它返回的函数大致等同于表达式产生的函数:

lambda x: x[1]

student_tuples[0] isn't added as an argument anywhere in itemgetter . student_tuples[0]不会作为参数添加到itemgetter任何位置。 It is passed as an argument to the function-that-was-returned when you call f(student_tuples[0]) . 调用f(student_tuples[0])时,它将作为参数传递给返回的函数。

Since f is the result of itemgetter(1) , it follows that you can do this in one line as: 由于fitemgetter(1)的结果,因此可以在一行中执行以下操作:

itemgetter(1)(student_tuples[0])

You need to call the return value of itemgetter() again, not pass in the list as the second argument. 您需要再次调用itemgetter()的返回值,而不是将列表作为第二个参数传递。 Example - 范例-

itemgetter(1)(student_tuples[0])

As can be seen from the documentation - 从文档中可以看出-

operator.itemgetter(*items) operator.itemgetter(*项)

Return a callable object that fetches item from its operand using the operand's __getitem__() method. 返回一个可调用对象,该对象使用操作数的__getitem__()方法从其操作数获取项目。

itemgetter() returns a function, which you can again call passing in the actual iterable, to get the value from that particular index in that iterable. itemgetter()返回一个函数,您可以再次调用传递实际的可迭代对象,以从该可迭代对象的特定索引中获取值。

When you pass in multiple values to itemgetter() , it still returns a function, and calling that function would try to get the elements from the iterable using the index you passed in to itemgetter() initially as a tuple. 当您将多个值传递给itemgetter() ,它仍然返回一个函数,调用该函数将尝试使用最初作为元组传递给itemgetter()的索引从可迭代对象中获取元素。 Example - 范例-

>>> l =[1,2,3,4,5]
>>> operator.itemgetter(1,2,4)(l)
(2, 3, 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM