嵌套列表上的最小/最大函数如何工作? [英] How does the min/max function on a nested list work?

查看:62
本文介绍了嵌套列表上的最小/最大函数如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说有一个嵌套列表,例如:

Lets say, there is a nested list, like:

my_list = [[1, 2, 21], [1, 3], [1, 2]]

在此函数上调用函数min()时:

When the function min() is called on this:

min(my_list)

收到的输出是

[1, 2]

为什么以及如何运作?有哪些用例?

Why and How does it work? What are some use cases of it?

推荐答案

如何在Python中比较列表和其他序列?

比较Python中的列表(和其他序列)

How are lists and other sequences compared in Python?

Lists (and other sequences) in Python are compared lexicographically and not based on any other parameter.

可以将序列对象与具有相同序列类型的其他对象进行比较.比较使用 lexicographical 顺序:首先比较前两个项目,如果它们不同,则确定比较的结果;如果它们相等,则比较下两个项目,依此类推,直到用尽任何一个序列.

Sequence objects may be compared to other objects with the same sequence type. The comparison uses lexicographical ordering: first the first two items are compared, and if they differ this determines the outcome of the comparison; if they are equal, the next two items are compared, and so on, until either sequence is exhausted.


什么是字典排序?

从Wikipedia页面上的词典排序


What is lexicographic sorting?

From the Wikipedia page on lexicographic sorting

词典顺序或词典顺序(也称为词典顺序,字典顺序,字母顺序或词典产品)是对单词字母顺序基于其组成字母的字母顺序的方式的概括.

lexicographic or lexicographical order (also known as lexical order, dictionary order, alphabetical order or lexicographic(al) product) is a generalization of the way the alphabetical order of words is based on the alphabetical order of their component letters.

min 函数返回 iterable .因此,[1,2]的词典顺序值在该列表中是最小的.您可以使用[1,2,21]

The min function returns the smallest value in the iterable. So the lexicographic value of [1,2] is the least in that list. You can check by using [1,2,21]

>>> my_list=[[1,2,21],[1,3],[1,2]]
>>> min(my_list)
[1, 2]


min的情况下会发生什么?

my_list上是明智的元素,首先是[1,2,21][1,3].现在从文档


What is happening in this case of min?

Going element wise on my_list, firstly [1,2,21] and [1,3]. Now from the docs

如果要比较的两个项目本身是同一类型的 序列 ,则按字典顺序比较 .

因此[1,1,21]的值小于[1,3],因为[1,3]的第二个元素(3)在字典上比的第二个元素的值 [1,1,21],即1.

Thus the value of [1,1,21] is less than [1,3], because the second element of [1,3], which is, 3 is lexicographically higher than the value of the second element of [1,1,21], which is, 1.

现在比较[1,2][1,2,21],并从文档中添加另一个引用

Now comparing [1,2] and [1,2,21], and adding another reference from the docs

如果一个序列是另一个序列的 初始子序列 ,则 较短序列是较小的 (较小).

If one sequence is an initial sub-sequence of the other, the shorter sequence is the smaller (lesser) one.

[1,2][1,2,21]的初始子序列.因此,[1,2]的值总体上小于[1,2,21]的值.因此,将[1,2]作为输出返回.

[1,2] is an initial sub-sequence of [1,2,21]. Therefore the value of [1,2] on the whole is smaller than that of [1,2,21]. Hence [1,2] is returned as the output.

这可以通过使用 sorted 函数

This can be validated by using the sorted function

>>> sorted(my_list)
[[1, 2], [1, 2, 21], [1, 3]]


如果列表包含多个最小元素怎么办?

如果列表包含重复的min元素则返回第一个


What if the list has multiple minimum elements?

If the list contains duplicate min elements the first is returned

>>> my_list=[[1,2],[1,2]]
>>> min(my_list)
[1, 2]

可以使用id函数调用确认

>>> my_list=[[1,2],[1,2]]
>>> [id(i) for i in my_list]
[140297364849368, 140297364850160]
>>> id(min(my_list))
140297364849368


我该怎么做才能防止min中的字典比较?

如果所需的比较不是不按字典顺序,则可以使用key参数(如


What do I need to do to prevent lexicographic comparison in min?

If the required comparison is not lexicographic then the key argument can be used (as mentioned by Padraic)

min函数具有一个称为key可选可选参数. key参数具有一个功能.

The min function has an additional optional argument called key. The key argument takes a function.

可选的key参数指定一个参数的排序函数 类似于list.sort()所使用的.关键参数(如果提供)必须为 以关键字形式(例如min(a,b,c,key=func)).

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)).

例如,如果我们需要长度最小的元素,则需要使用

For example, if we need the smallest element by length, we need to use the len function.

>>> my_list=[[1,2,21],[1,3],[1,2]]
>>> min(my_list,key=len)            # Notice the key argument
[1, 3]

我们可以看到这里返回了第一个最短元素.

As we can see the first shortest element is returned here.

直到Python2

Until Python2

如果列表是异类的 类型名称 ,则应进行排序,请检查

除数字以外的其他类型的对象按其 类型名称

Objects of different types except numbers are ordered by their type names

因此,如果放置intlist,则将得到整数值,因为i的值小于l的最小值.类似地,'1'的值将比这两个值高.

Hence if you put an int and a list there you will get the integer value as the smallest as i is of lower value than l. Similarly '1' would be of higher value than both of this.

>>> my_list=[[1,1,21],1,'1']
>>> min(my_list)
1

Python3及更高版本

Python3 and onwards

但是,在 Python3 中删除了这种令人困惑的技术.现在引发TypeError .阅读 Python 3.0的新增功能

However this confusing technique was removed in Python3. It now raises a TypeError. Read What's new in Python 3.0

当操作数没有有意义的自然排序时,排序比较运算符(<<=>=>)会引发TypeError异常.因此,1 < ''0 > Nonelen <= len之类的表达式不再有效,例如None < None引发TypeError而不是返回False.必然的结果是, 对异构列表进行排序不再有意义-所有元素必须彼此可比. .

The ordering comparison operators (<, <=, >=, >) raise a TypeError exception when the operands don’t have a meaningful natural ordering. Thus, expressions like 1 < '', 0 > None or len <= len are no longer valid, and e.g. None < None raises TypeError instead of returning False. A corollary is that sorting a heterogeneous list no longer makes sense – all the elements must be comparable to each other.

>>> my_list=[[1,1,21],1,'1']
>>> min(my_list)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < list()

但是它适用于可比较类型,例如

>>> my_list=[1,2.0]
>>> min(my_list)
1

在这里我们可以看到list包含float值和int值.但是由于floatint是可比较的类型,因此min函数在这种情况下有效.

Here we can see that the list contains float values and int values. But as float and int are comparable types, min function works in this case.

这篇关于嵌套列表上的最小/最大函数如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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