python关键字函数,最小/最大Python的关键字函数

I am trying to understand how this works:

my_dict = {'a':2,'b':1}

min(my_dict, key=my_dict.get)

produces

b

Which is a really cool feature and one I want to understand better.

Based on the 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)).

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

Edit: I came across this today:

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

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

解决方案

The code you have written is

my_dict = {'a':2,'b':1}

min(my_dict, key=my_dict.get)

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.

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]

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.

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.

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

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

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.

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), where thing is either an iterator or a sequence, returns a iterator that will return (0, thing[0]), (1, thing1), (2, thing[2])

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.

you can also use lambda function of here like

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

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关键字Python已经使用的、不允许开发人员重复定义的标识符。Python3中已经定义了35个关键字,这些关键字都存储在keyword模块的变量kwlist中,可以通过导入keyword模块并查看kwlist变量来查看Python中的关键字函数Python中的一种可重复使用的代码块,用于执行特定的任务。Python中有许多内置函数,其中包括数学运算函数、类型转换函数、序列操作函数、对象操作函数、交互操作函数和文件操作函数等。 数学运算函数是用于执行数学运算的函数。其中,pow()函数用于计算一个数的幂。其他的数学运算函数还包括abs()、divmod()、max()、min()、round()和sum()等。 类型转换函数用于进行不同类型之间的转换。常用的类型转换函数有bool()、int()、float()和str()等。 序列操作函数用于操作序列类型的数据,如列表和元组等。常用的序列操作函数有all()、any()、filter()、map()和sorted()等。 对象操作函数用于操作对象,如查看对象的帮助文档、获取对象的属性和类型、格式化对象等。常用的对象操作函数有help()、dir()、type()和format()等。 交互操作函数用于与用户进行交互,如打印输出和获取用户输入。其中,print()函数用于打印输出内容到控制台,而input()函数用于获取用户的输入。 文件操作函数用于对文件进行操作,如打开文件、读取文件内容等。其中,open()函数用于打开文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Python - Python关键字和内置函数及其用法(持续记录更新)](https://blog.csdn.net/Naiva/article/details/90745661)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值