我有以下代码:
>>> 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]
给出正常有序的pairs
)。
我们到底是如何访问只存在于可怕的lambda函数的不纯约束中的虚无的?此外,当我们凝视虚无时,进入虚无是如何使凝视我们的虚无回归的?
# 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')]
我不知道你认为lambda函数是什么,但它只不过是一个简单的单行函数。你的代码可以很容易地写成这样:
def sort_key(pair):
return pair[1]
pairs.sort(key=sort_key)
正如你所看到的,没有什么是凭空变出来的;CCD_ 5只是函数的参数。
它有一个索引,因为sort
将关键字函数传递给要排序的列表的每个元素;所以在第一个调用中,pair
将是(1, 'one')
,依此类推。
这完全可以正常工作,因为您指定了第一个索引作为键,所以元组将根据字符串进行排序,按字母顺序排序。索引0实际上是第一个元素,其中索引1是第二个元素,因为索引从零开始计数。因此,当您提供lambda时,您告诉sort方法,根据元组的第二个元素对元组进行排序,这些元素都是字符串。