简体   繁体   English

Python lambda 函数下划线-冒号语法解释?

[英]Python lambda function underscore-colon syntax explanation?

在以下“aDict”是字典的 Python 脚本中,“_: _[0]”在 lambda 函数中做了什么?

sorted(aDict.items(), key=lambda _: _[0])

In Python _ (underscore) is a valid identifier and can be used as a variable name, eg在 Python 中_ (下划线)是一个有效的标识符,可以用作变量名,例如

>>> _ = 10
>>> print(_)
10

It can therefore also be used as the name of an argument to a lambda expression - which is like an unnamed function.因此,它也可以用作 lambda 表达式的参数名称 - 这就像一个未命名的函数。

In your example sorted() passes tuples produced by aDict.items() to its key function.在您的示例中sorted()aDict.items()生成的元组传递给它的key函数。 The key function returns the first element of that tuple which sorted() then uses as the key, ie that value to be compared with other values to determine the order. key 函数返回该元组的第一个元素, sorted()然后将其用作键,即要与其他值进行比较以确定顺序的值。

Note that, in this case, the same result can be produced without a key function because tuples are naturally sorted according to the first element, then the second element, etc. So请注意,在这种情况下,可以在没有键函数的情况下产生相同的结果,因为元组是根据第一个元素自然排序的,然后是第二个元素,依此类推。所以

sorted(aDict.items())

will produce the same result.将产生相同的结果。 Because dictionaries can not contain duplicate keys, the first element of each tuple is unique, so the second element is never considered when sorting.因为字典不能包含重复的键,每个元组的第一个元素是唯一的,所以排序时从不考虑第二个元素。

Lets pick that apart.让我们把它分开。

1) Suppose you have a dict, di: 1) 假设你有一个 dict, di:

di={'one': 1, 'two': 2, 'three': 3}

2) Now suppose you want each of its key, value pairs: 2)现在假设你想要它的每个键值对:

 >>> di.items()
 [('three', 3), ('two', 2), ('one', 1)]

3) Now you want to sort them (since dicts are unordered): 3)现在你想对它们进行排序(因为字典是无序的):

>>> sorted(di.items())
[('one', 1), ('three', 3), ('two', 2)]

Notice that the tuples are sorted lexicographically -- by the text in the first element of the tuple.请注意,元组按字典顺序排序——按元组第一个元素中的文本排序。 This is a equivalent to the t[0] of a series of tuples.这相当于一系列元组的t[0]

Suppose you wanted it sorted by the number instead.假设您希望它按数字排序。 You would you use a key function:你会使用一个key函数:

>>> sorted(di.items(), key=lambda t: t[1])
[('one', 1), ('two', 2), ('three', 3)]

The statement you have sorted(aDict.items(), key=lambda _: _[0]) is just using _ as a variable name.sorted(aDict.items(), key=lambda _: _[0])的语句sorted(aDict.items(), key=lambda _: _[0])只是使用_作为变量名。 It also does nothing, since aDict.items() produces tuples and if you did not use a key it sorts by the first element of the tuple anyway.它也不做任何事情,因为aDict.items()产生元组,如果你没有使用键,它无论如何都会按元组的第一个元素排序。 The key function in your example is completely useless.您示例中的 key 函数完全没用。

There might be a use case for the form (other than for tuples) to consider.表单(元组除外)可能需要考虑一个用例。 If you had strings instead, then you would be sorting by the first character and ignoring the rest:如果您有字符串,那么您将按第一个字符排序并忽略其余字符:

>>> li=['car','auto','aardvark', 'arizona']
>>> sorted(li, key=lambda c:c[0])
['auto', 'aardvark', 'arizona', 'car']

Vs:对比:

>>> sorted(li)
['aardvark', 'arizona', 'auto', 'car']

I still would not use _ in the lambda however.但是,我仍然不会在 lambda 中使用_ The use of _ is for a throway variable that has minimal chance of side-effects. _的使用是用于具有最小副作用机会的 throway 变量。 Python has namespaces that mostly makes that worry not a real worry. Python 的命名空间主要使这种担心不是真正的担心。

Consider:考虑:

>>> c=22
>>> sorted(li, key=lambda c:c[0])
['auto', 'aardvark', 'arizona', 'car']
>>> c
22

The value of c is preserved because of the local namespace inside the lambda .由于lambda内部的本地命名空间, c的值得以保留。

However (under Python 2.x but not Python 3.x) this can be a problem:但是(在 Python 2.x 但不是 Python 3.x 下)这可能是一个问题:

>>> c=22
>>> [c for c in '123']
['1', '2', '3']
>>> c
'3'

So the (light) convention became using _ for a variable either in the case of a list comprehension or a tuple expansion, etc where you worry less about trampling on one of your names.因此(轻量级)约定变成在列表理解或元组扩展等情况下使用_作为变量,您不必担心践踏您的名字之一。 The message is: If it is named _ , I don't really care about it except right here...消息是:如果它被命名为_ ,我真的不关心它,除了在这里......

In Python, lambda is used to create an anonymous function.在 Python 中,lambda 用于创建匿名函数。 The first underscore in your example is simply the argument to the lambda function.您示例中的第一个下划线只是 lambda 函数的参数。 After the colon (ie function signature), the _[0] retrieves the first element of the variable _ .在冒号(即函数签名)之后, _[0]检索变量_的第一个元素。

Admittedly, this can be confusing;诚然,这可能令人困惑。 the lambda component of your example could be re-written as lambda x: x[0] with the same result.您示例的 lambda 组件可以重写为lambda x: x[0] ,结果相同。 Conventionally, though, underscore variable names in Python are used for "throwaway variables".但是,通常情况下,Python 中的下划线变量名称用于“一次性变量”。 In this case, it implies that the only thing we care about in each dictionary item is the key.在这种情况下,它意味着我们在每个字典项中唯一关心的就是键。 Nuanced to a fault, perhaps.细微到一个错误,也许。

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

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