python(ubuntu 16.04.1上的2.7.12版)-函数(类似于返回元素2的lambda)不能作为排序的键

python(ubuntu 16.04.1上的2.7.12版)-函数(类似于返回元素2的lambda)不能作为排序的键,python,function,sorting,lambda,key,Python,Function,Sorting,Lambda,Key,我正在试验排序键,并尝试将lambdas与函数进行比较,以尝试了解lambdas是如何工作的,以及sorted如何将数据传递给lambda中的可回放参数 当我尝试使用函数而不是lambda时,有人能解释一下我在这里做错了什么吗?如果使用函数,我关于参数如何从排序键传递到lambda变量的假设似乎是无效的 请查看下面的我的代码及其下面的输出… 这是我的密码: #!/usr/bin/python #-------------------------- sep = "\n-------------

我正在试验排序键,并尝试将lambdas与函数进行比较,以尝试了解lambdas是如何工作的,以及sorted如何将数据传递给lambda中的可回放参数

当我尝试使用函数而不是lambda时,有人能解释一下我在这里做错了什么吗?如果使用函数,我关于参数如何从排序键传递到lambda变量的假设似乎是无效的

请查看下面的我的代码及其下面的输出…

这是我的密码:

#!/usr/bin/python

#--------------------------

sep = "\n----------------\n"

#--------------------------

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

#--------------------------

print sep, "plain student_tuples on each line"
for x in student_tuples:
    print x, type(x)

#--------------------------

print sep, "show lambda is returning element 2 of each nested tuple"

for line in student_tuples:
    ld = (lambda x: x[2])(line)
    print ld, type(ld)

#--------------------------

print sep, "show sorted is passing each tuple to lambda in key="

st = sorted(student_tuples, key=lambda x: x[2])

for s in st:
    print s

#   the above suggests (to me), that key=whatever in sorted is passing 
#   each element (or nested tuple) from the parent tuple 
#   into the replacable x parameter of the lambda, (which returns element 2.) 
#
#   therefore, I should be able to replace the lambda with a function 
#   that does the same thing, and the key= part of sorted should pass 
#   each tuple to the replacable paramter of the function too.

#--------------------------

#       define a function that should do the same as the lambda
def slice_2(a):
    return a[2]

#--------------------------

print sep, "function, slice_2 on its own for student_tuples"

for line in student_tuples:
    s2 = slice_2(line)
    print s2, type(s2)

#--------------------------

print sep, "sorted should pass data into slice_2 functions replacable paramter"

sf = sorted( student_tuples, key=slice_2(y) )

for l in sf:
    print l


#--------------------------

#################
# end of script #
#################
以下是脚本的输出,但出现异常错误:

----------------
plain student_tuples on each line
('john', 'A', 15) <type 'tuple'>
('jane', 'C', 10) <type 'tuple'>
('dave', 'D', 12) <type 'tuple'>

----------------
show lambda is returning element 2 of each nested tuple
15 <type 'int'>
10 <type 'int'>
12 <type 'int'>

----------------
show sorted is passing each tuple to lambda in key=
('jane', 'C', 10)
('dave', 'D', 12)
('john', 'A', 15)

----------------
function, slice_2 on its own for student_tuples
15 <type 'int'>
10 <type 'int'>
12 <type 'int'>

----------------
sorted should pass data into slice_2 functions replacable paramter
Traceback (most recent call last):
  File "./compare-tuple-to-function.py", line 88, in <module>
    sf = sorted( student_tuples, key=slice_2(y) )
NameError: name 'y' is not defined
----------------
每行上的普通学生元组
(“约翰”,“A”,“15”)
('jane','C',10)
('dave','D',12)
----------------
show lambda返回每个嵌套元组的元素2
15
10
12
----------------
show sorted正在将每个元组传递给键中的lambda=
('jane','C',10)
('dave','D',12)
(“约翰”,“A”,“15”)
----------------
函数,为student_元组单独切片_2
15
10
12
----------------
排序后应将数据传递到slice_2函数的可重放参数中
回溯(最近一次呼叫最后一次):
文件“/compare tuple to function.py”,第88行,在
sf=排序(学生组,键=切片2(y))
名称错误:未定义名称“y”

使用
key=slice_2
,而不是
key=slice_2(y)
。您需要使用函数本身作为键,而不是使用不存在的神秘的
y
调用函数的结果。

使用
key=slice\u 2
,而不是
key=slice\u 2(y)
。您需要使用函数本身作为键,而不是使用不存在的神秘的
y
调用函数的结果。

只需将函数(名称)作为键,而不是任何(y)参数。该函数将以当前元素作为单个参数自动调用

sf = sorted(student_tuples, key=slice_2)

这将给出预期的输出。

您只需将函数(名称)作为键,而不是任何(y)参数。该函数将以当前元素作为单个参数自动调用

sf = sorted(student_tuples, key=slice_2)

这将给出预期的输出。

嗨,Stefan,非常感谢您的回答。现在可以正常工作了。嗨,Stefan,非常感谢你的回答。它现在可以正常工作了。嗨,克里斯蒂安,谢谢你的回答。。。。我没有意识到在这种情况下函数语法需要是“key=slice_2”。(与其他地方使用“result=slice_2(y)syntax”不同)——我仍然在学习Python。。。。多亏了你们两位,区别在于:slice_2(y)调用函数,然后函数返回一个值。因此,通常,当您执行
result=slice_2(y)
时,您“简单地”将函数的返回值分配给变量result。但这里您不需要特定的返回值。相反,您希望为sorted()提供一个函数,该函数可用于获取排序。但是,是的,这一点一开始可能看起来很奇怪。在你解释之前,我认为key=以某种方式将参数传递给函数,现在我了解key=func或key=lambda,将函数或lambda传递给sorted,这是一个非常根本的区别。(因此,我很值得去尝试和理解key=的功能)。再次感谢!嗨,克里斯蒂安,谢谢你的回答。。。。我没有意识到在这种情况下函数语法需要是“key=slice_2”。(与其他地方使用“result=slice_2(y)syntax”不同)——我仍然在学习Python。。。。多亏了你们两位,区别在于:slice_2(y)调用函数,然后函数返回一个值。因此,通常,当您执行
result=slice_2(y)
时,您“简单地”将函数的返回值分配给变量result。但这里您不需要特定的返回值。相反,您希望为sorted()提供一个函数,该函数可用于获取排序。但是,是的,这一点一开始可能看起来很奇怪。在你解释之前,我认为key=以某种方式将参数传递给函数,现在我了解key=func或key=lambda,将函数或lambda传递给sorted,这是一个非常根本的区别。(因此,我很值得去尝试和理解key=的功能)。再次感谢!