恶意的Python仪式在他们不应该工作时工作

时间:2015-03-05 12:12:23

标签: python python-3.x lambda

我有以下代码:

>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
>>> pairs.sort(key=lambda pair: pair[1])
>>> pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]

我刚刚接受了lambda函数,我得到了它们的工作方式,至少,我能理解他们恶魔黑暗魔法背后的邪恶仪式。

但即使使用Python代码让我疯狂,我仍然不知道为什么这段代码有效? pair没有以任何方式定义,所以为什么存在访问索引,更糟糕的是,为什么该索引很重要(fyi pair[2]超出范围而pair[0]给出正常的有序{{ 1}})。

我们到底怎样才能获得仅存在于可怕的lambda函数的不纯约束中的虚无?此外,当我们凝视它时,访问虚无会如何回归任何隐藏在我们身上的空洞?

3 个答案:

答案 0 :(得分:6)

# The following incantation shall summon from the depths
# of the data abyss thirteen entities: four tuples,
# four integers, four strings and a list containing all these.
# The amalgamation of these entities, the list, shall be bound
# to the name `pairs` to further do our dark bidding without
# fleeing into nothingness.

pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]

# One of the 133,316,666 demons is also bound by the spirit of
# Tim with the "member" `sort`. Another way to call upon the spirit
# of Tim is the `sorted` name, though it will use its dark magic
# to invoke another copy of the list, and thus would waste our magics
# unnecessarily.
# The sort demon can be wrangled to not compare things by their inherent
# value, but by another incantation, a `function`. The demon will then
# invoke that incantation for each of the things it compares, and use
# that value for sorting.
# (Before Python III, there also used to be another way, a "comparison"
# function, but this way is easier, which is likely why it was banished.
# Why else be a wizard if one did not wish ease and comfort?)
# The `lambda` word of power is equivalent to the more familiar `def`
# form, as such:
#
#    lambda pair:    |  def ANONYMOUS(pair):
#      pair[1]       |    return pair[1]
#
# The similarities and differences are quite easy to spot.
# 
# And indeed, if one were to `def ANONYMOUS`, they could form this incan-
# tation as `key=ANONYMOUS` instead.
#
# And for, as we mentioned earlier, the dark things confined within the
# list bound to the name `pairs` are tuples, which further confine within
# themselves an integer and string each, all accessible to our hands by
# indexing by zero (which is the Only True Way) by the bracket sigil [x],
# it only makes sense for the function to do just that to pass the value
# to the sorting demon that way.
# An illustration of this "indexing" for the fledgling wizard:
#
# [             | pairs
#    (          | pairs[0]
#        1,     | pairs[0][0]
#       'one'   | pairs[0][1]
#    ),
#    (          | pairs[1]
#       2,      | pairs[1][0]
#       'two'   | pairs[1][1]
#    )
# ]
#

pairs.sort(key=lambda pair: pair[1])

# And so, the mage may laugh and enjoy themselves, watching the demon
# futilely sort his items, and eventually we may gaze upon the fruits
# of his effort. Not the demon's, of course, for it is but a tool for the
# great wizard.

pairs
[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]

答案 1 :(得分:2)

我不知道你认为lambda函数是什么,但它只不过是一个简单的单行函数。您的代码可以像这样轻松编写:

def sort_key(pair):
    return pair[1]

pairs.sort(key=sort_key)

正如你所看到的,没有任何东西是凭空而来的。 pair只是函数的参数。

它有一个索引,因为sort传递了要排序的列表中每个元素的键函数;因此,在第一次通话中,pair将为(1, 'one'),依此类推。

答案 2 :(得分:0)

这完全正常,因为您指定了第一个索引作为键,元组将根据字符串进行排序,按字母顺序排序。索引零实际上是第一个元素,其中索引1是第二个元素,因为索引从零开始计数。因此,当你提供lambda时,你会告诉sort方法,用它们的第二个元素对元组进行排序,这两个元素都是字符串。