- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想了解这是如何工作的:
my_dict = {'a':2,'b':1}
min(my_dict, key=my_dict.get)
产生
b
这是一个非常酷的功能,我想更好地理解它。
基于documentation
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)).
在哪里可以找到有关可用功能的更多信息?如果是字典,是不是都是字典方法?
编辑:我今天遇到了这个:
max(enumerate(array_x), key=operator.itemgetter(1))
仍在寻找有关 min/max 的可用关键字函数的信息
最佳答案
你写的代码是
my_dict = {'a':2,'b':1}
min(my_dict, key=my_dict.get)
实际上这适用于 min
函数。那么,min 是做什么的?
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.
这里的key是用来传递自定义比较函数的。
示例:按列表长度输出最大值,其中 arg1、arg2 均为列表。
>>>> max([1,2,3,4], [3,4,5], key=len)
[1, 2, 3, 4]
但是如果我想要列表中的最大值,但要考虑元组的第二个元素怎么办?这里我们可以使用函数,官方给的documentation . def
语句是复合语句,它们不能用在需要表达式的地方,这就是有时使用 lambda 的原因。
请注意,lambda 等同于您在 def 的返回语句中输入的内容。因此,您不能在 lambda 中使用语句,只允许使用表达式。
>>> max(l, key = lambda i : i[1])
(1, 9)
# Or
>>> import operator
>>> max(l, key = operator.itemgetter(1))
(1, 9)
所以函数基本上取决于可迭代对象和传递比较标准。
现在在您的示例中,您正在遍历字典。在 key 中,您在这里使用 get
方法。
The method get() returns a value for the given key. If key is not available then returns default value None.
在这里,get
方法中没有参数,它只是迭代字典的值。因此 min
为您提供具有最小值的 key 。
对于 max(enumerate(array_x), key=operator.itemgetter(1))
我们想比较数组的值而不是它们的索引。所以我们枚举了这个数组。
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)
构造一个可调用对象,假定可迭代对象(例如列表、元组、集合)作为输入,并从中获取第 n 个元素。
你也可以像这里一样使用lambda函数
max(enumerate(array_x), key=lambda i: i[1])
所以key
中的功能范围差不多就可以使用了。我们可以使用许多功能,但唯一的动机是,它是比较的标准。
关于python - Python 最小/最大关键字函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36502505/
如果我创建一个对象时没有使用 new 关键字,例如“Object s(someval)”,但该对象的构造函数使用了 new,当该对象超出范围时,是否会调用析构函数为其分配新的空间?我感觉好像是,但我不
在 SQL 语法中,我发现奇怪的规则表明 select * from ONLY (t1)是有效的 SQL。 我的问题是:什么是 ONLY在这种情况下是什么意思? 它在规范的“7.6 table ref
为什么使用 $(this) 而不是重新选择类很重要? 我在代码中使用了大量的动画和 CSS 编辑,并且我知道可以使用 $(this) 来简化它。 最佳答案 当您通过 jQuery 执行 DOM 查询(
我正在尝试使用 IN 关键字编写查询。 表A 属性标识、属性名称 表B key 、属性标识、属性值 根据提供的 key ,我想返回所有 attrName、attrVal 组合。结果将包含两个表中的列。
这个问题在这里已经有了答案: Why would you use "AS" when aliasing a SQL table? (8 个答案) 关闭 9 年前。 我不擅长写查询,但是从我开始使用
我读过,在 Java 中,您不必将 this 关键字显式绑定(bind)到对象,它由解释器完成。它与 Javascript 相反,在 Javascript 中你总是必须知道 this 的值。但是 Ja
Swift 中“with”关键字的用途是什么?到目前为止,我发现如果您需要覆盖现有的全局函数,例如 toDebugString,可以使用该关键字。 // without "with" you
这个问题在这里已经有了答案: What does the keyword "where" in a class declaration do? (7 个答案) 关闭 9 年前。 在下面的一段代码中(
免责声明:swift 菜鸟 您好,我刚刚开始学习 Swift,正在学习 Swift 编程语言(Apple 在 WWDC 期间发布的书籍),并且想知道“where”关键字是什么。它用于 let vege
深入研究文档后,我找不到以下问题的答案: 是否有任何理由反对使用 this 来引用当前对象,如下例所示? type MyStruct struct { someField string } fun
前言 最近在做THINKPHP开发项目中,用到了 parent:: 关键字,实际上 parent::关键字 是PHP中常要用到的一个功能,这不仅仅是在 THINKPHP 项目开发中,即使是一个小型
我们都知道且经常用到 unsigned 关键字,但有没有想过,与此对应的 signed 关键字有啥用? 复制代码 代码如下: int i = 0; signed
this关键字再java里面是一个我认为非常不好理解的概念,:)也许是太笨的原因 this 关键字的含义:可为以调用了其方法的那个对象生成相应的句柄。 怎么理解这段话呢? thinking i
一 什么是 synchronized synchronized 关键字提供了一种锁机制,能够确保共享变量互斥访问,从而防止数据不一致问题的出现。 synchronized 关键字包括 monitor
最近看了几篇 synchronized 关键字的相关文章,收获很大,想着总结一下该关键字的相关内容。 1、synchronized 的作用 原子性:所谓原子性就是指一个操作或者多个操作,要么全部执行并
在本教程中,您将借助示例了解 JavaScript 对象方法和 this 关键字。 在 JavaScript 中,对象也可以包含函数。例如, // object containing meth
有人可以解释一下 PHP“with”的作用吗? 示例开始: 假设我有一个类: \App\fa_batch 这句话有什么区别: $w = (with (new \App\fa_batch))
这个问题在这里已经有了答案: What is the difference between using the colon and as syntax for declaring type? (2
如果我在 WHERE 子句中使用以下任一项,是否会有很大不同: WHERE [Process Code] = 1 AND ([Material ID] = 'PLT' OR [Material ID]
This question is unlikely to help any future visitors; it is only relevant to a small geographic are
我是一名优秀的程序员,十分优秀!