最小/最大Python的关键字函数 [英] Keyword functions for Python min/max

查看:91
本文介绍了最小/最大Python的关键字函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解其工作原理:

I am trying to understand how this works:

my_dict = {'a':2,'b':1}
min(my_dict, key=my_dict.get)

产生

b

这是一个非常酷的功能,我想更好地理解它.
基于文档

Which is a really cool feature and one I want to understand better.
Based on the documentation

min(iterable [,键]) 返回可迭代的最小项或两个或多个参数中的最小项... 可选的key参数指定一个单参数排序函数,如用于list.sort()的函数.如果提供了key参数,则必须采用关键字形式(例如min(a,b,c,key = func)).

min(iterable[, key]) Return the smallest item in an iterable or the smallest of two or more arguments... The optional key argument specifies a one-argument ordering function like that used for list.sort(). The key argument, if supplied, must be in keyword form (for example, min(a,b,c,key=func)).

在哪里可以找到有关可用功能的更多信息?就字典而言,是所有的字典方法吗?

Where can I find out more about available functions? In the case of a dictionary, is it all the dictionary methods?

我今天遇到了这个问题:

I came across this today:

max(enumerate(array_x), key=operator.itemgetter(1))

仍在寻找有关最小/最大可用关键字功能的信息

Still looking for information on available keyword functions for min/max

推荐答案

您编写的代码是

my_dict = {'a':2,'b':1}
min(my_dict, key=my_dict.get)

实际上这对min功能有效. 那min怎么办?

actually this works on min function. so, what does min do?

min(a, b, c, ...[, key=func]) -> value

使用单个可迭代参数,返回其最低的项目.如果有两个或多个参数,则返回最低的参数.

With a single iterable argument, return its lowest item. With two or more arguments, return the lowest argument.

此处的键用于传递自定义比较功能.

The key here is used to pass a custom comparison function.

示例:按列表长度输出最大值,其中arg1,arg2均为列表.

Example: output max by length of list, where arg1, arg2 are both lists.

>>>> max([1,2,3,4], [3,4,5], key=len)
[1, 2, 3, 4]

但是如果我想从列表中获得最大值,但考虑元组的第二个元素怎么办?在这里,我们可以使用官方文档中给出的函数. def语句是复合语句,不能在需要表达式的地方使用,这就是为什么有时使用 lambda 的原因.

But what if I want the max from the list, but by considering the second element of the tuple? here we can use functions, as given in official documentation. The def statements are compound statements they can't be used where an expression is required, that's why sometimes lambda's are used.

请注意,lambda等效于您在def的return语句中输入的内容.因此,您不能在lambda中使用语句,仅允许使用表达式.

Note that lambda is equivalent to what you'd put in a return statement of a def. Thus, you can't use statements inside a lambda, only expressions are allowed.

>>> max(l, key = lambda i : i[1])
(1, 9)

# Or

>>> import operator
>>> max(l, key = operator.itemgetter(1))
(1, 9)

所以功能基本上取决于可迭代性和通过比较的标准.

so the functions are basically depend upon the the iterable and and passing the criteria for the comparison.

现在在您的示例中,您正在遍历字典.在 key 中,您在这里使用get方法.

Now in your example, you are iterating over your dictionary. And in key, you are using get method here.

方法get()返回给定键的值.如果密钥不可用,则返回默认值None.

The method get() returns a value for the given key. If key is not available then returns default value None.

如此处所示,get方法中不存在任何参数,它仅对字典的值进行迭代.因此,min为您提供具有最小值的键.

As here, no arguments are there in get method it simply iterates over values of dictionary. And thus the min gives you the key having minimum value.

对于max(enumerate(array_x), key=operator.itemgetter(1)) 我们想比较数组的值而不是它们的索引.因此,我们枚举了数组.

For max(enumerate(array_x), key=operator.itemgetter(1)) we want to compare the values of array instead of their indices. So we have enumerated the array.

enumerate(thing),其中Thing是迭代器或序列,返回一个迭代器,该迭代器将返回(0,Thing [0]),(1,Thing

enumerate(thing), where thing is either an iterator or a sequence, returns a iterator that will return (0, thing[0]), (1, thing1), (2, thing[2])

现在我们已经使用了 operator 模块的itemgetter功能. operator.itemgetter(n)构造一个以可迭代对象(例如list,tuple,set)为输入的可调用对象,并从其中获取第n个元素.

now we have used itemgetter function of operator module. operator.itemgetter(n) constructs a callable that assumes an iterable object (e.g. list, tuple, set) as input, and fetches the n-th element out of it.

您还可以像这样使用lambda函数

you can also use lambda function of here like

max(enumerate(array_x), key=lambda i: i[1])

因此,key中的功能范围几乎可以使用.我们可以使用许多功能,但唯一的动机是,这是进行比较的标准.

So the range of functions in key is almost up to the use. we can use many functions but the sole motive is , it is the criteria for that comparison.

这篇关于最小/最大Python的关键字函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆