繁体   English   中英

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

[英]python (version 2.7.12 on ubuntu 16.04.1) - a function (similar to a lambda to return element 2) does not work as a key to sorted

我正在试验排序键,并尝试将lambda与函数进行比较,以尝试了解lambda如何工作,以及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

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

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

sf = sorted(student_tuples, key=slice_2)

这应该给出预期的输出。

暂无
暂无

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

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