英文文档:

min(iterable, *[, key, default])

min(arg1, arg2, *args[, key])

Return the smallest item in an iterable or the smallest of two or more arguments.

If one positional argument is provided, it should be an iterable. The smallest item in the iterable is returned. If two or more positional arguments are provided, the smallest of the positional arguments is returned.

There are two optional keyword-only arguments. The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised.

If multiple items are minimal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc)[0] and heapq.nsmallest(1, iterable, key=keyfunc).

说明:

  

  1. 函数功能为取传入的多个参数中的最小值,或者传入的可迭代对象元素中的最小值。默认数值型参数,取值小者;字符型参数,取字母表排序靠前者。还可以传入命名参数key,其为一个函数,用来指定取最小值的方法。default命名参数用来指定最小值不存在时返回的默认值。功能与max函数相反。

  2. 函数至少传入两个参数,但是有只传入一个参数的例外,此时参数必须为可迭代对象,返回的是可迭代对象中的最小元素。

>>> min(1) # 传入1个参数报错
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
min(1)
TypeError: 'int' object is not iterable
>>> min(1,2) # 传入2个参数 取2个中较小者
1
>>> min(1,2,3) # 传入3个参数 取3个中较小者
1
>>> min('') # 传入1个可迭代对象,取其最小元素值
''

  3. 当传入参数为数据类型不一致时,传入的所有参数将进行隐式数据类型转换后再比较,如果不能进行隐式数据类型转换,则会报错。

>>> min(1,1.1,1.3e1) # 整数与浮点数可取最小值
1
>>> min(1,2,'') # 数值与字符串不能取最小值
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
min(1,2,'')
TypeError: unorderable types: str() < int() >>> min([1,2],[1,3]) # 列表与列表可取最小值
[1, 2]
>>> min((1,2),[1,3]) # 列表与元组不能取最小值
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
min((1,2),[1,3])
TypeError: unorderable types: list() < tuple()

  4. 当存在多个相同的最小值时,返回的是最先出现的那个最小值。

#定义a、b、c 3个列表
>>> a = [1,2]
>>> b = [1,3]
>>> c = [1,2] #查看a、b、c 的id
>>> id(a)
66486184
>>> id(b)
66486224
>>> id(c)
66486024 #取最小值
>>> d = min(a,b,c)
>>> id(d)
66486184 #验证是否最小值是否是a
>>> id(a) == id(d)
True

  5. 默认数值型参数,取值小者;字符型参数,取字母表排序靠前者;序列型参数,则依次按索引位置的值进行比较取最小者。还可以通过传入命名参数key,指定取最小值方法。

>>> min(1,2) # 取数值小者
1
>>> min('a','b') # 取排序靠前者
'a'
>>> min('ab','ac','ad') # 依次按索引比较取较小者
'ab' >>> min(-1,-2) # 数值默认去数值较小者
-2
>>> min(-1,-2,key = abs) # 传入了求绝对值函数,则参数都会进行求绝对值后再取较小者
-1

  6. key参数的另外一个作用是,不同类型对象本来不能比较取最小值的,传入适当的key函数,变得可以比较能取最小值了。

>>> min(1,2,'') #数值和字符串不能取最小值
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
min(1,2,'')
TypeError: unorderable types: str() < int()
>>> min(1,2,'',key = int) # 指定key为转换函数后,可以取最小值
1 >>> min([1,2],(1,1)) #元组和列表不能取最小值
Traceback (most recent call last):
File "<pyshell#24>", line 1, in <module>
min([1,2],(1,1))
TypeError: unorderable types: tuple() < list()
>>> min([1,2],(1,1),key = lambda x:x[1]) #指定key为返回序列索引1位置的元素后,可以取最小值
(1, 1)

  7. 当只传入的一个可迭代对象时,而且可迭代对象为空,则必须指定命名参数default,用来指定最小值不存在时,函数返回的默认值。

>>> min(()) #空可迭代对象不能取最小值
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
min(())
ValueError: min() arg is an empty sequence >>> min((),default = 0) #空可迭代对象,指定default参数为默认值
0 >>> min((),0) #默认值必须使用命名参数进行传参,否则将被认为是一个比较的元素
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
min((),0)
TypeError: unorderable types: int() < tuple()

Python内置函数(43)——min的更多相关文章

  1. Python内置函数(4)——min

    英文文档: min(iterable, *[, key, default]) min(arg1, arg2, *args[, key]) Return the smallest item in an ...

  2. Python内置函数(43)——type

    英文文档: class type(object) class type(name, bases, dict) With one argument, return the type of an obje ...

  3. Python之路(第八篇)Python内置函数、zip()、max()、min()

    一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算,如果全部都是true,就返回true, 但是如果是空字符串.空列表也返回t ...

  4. Python之路Python内置函数、zip()、max()、min()

    Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...

  5. 【转】python 内置函数总结(大部分)

    [转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...

  6. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  7. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  8. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  9. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

随机推荐

  1. 数据分析入门——Pandas类库基础知识

    使用python进行数据分析时,经常会用Pandas类库处理数据,将数据转换成我们需要的格式.Pandas中的有两个数据结构和处理数据相关,分别是Series和DataFrame. Series Se ...

  2. 029 Es面试小节

    1.大纲 Es是什么?处理哪种业务逻辑用的多? Es类比数据库是什么? 对于数据库的字段.表等,在es中叫什么? Es的refresh把数据写到哪里? Es的数据如何变成检索和聚合索引的? Es的fl ...

  3. 蓝桥杯 黄金连分数(BigDecimal的使用)

    标题: 黄金连分数 黄金分割数0.61803... 是个无理数,这个常数十分重要,在许多工程问题中会出现.有时需要把这个数字求得很精确. 对于某些精密工程,常数的精度很重要.也许你听说过哈勃太空望远镜 ...

  4. CodeForces 510C Fox And Names (拓扑排序)

    <题目链接> 题目大意: 给你一些只由小写字母组成的字符串,现在按一定顺序给出这些字符串,问你怎样从重排字典序,使得这些字符串按字典序排序后的顺序如题目所给的顺序相同. 解题分析:本题想到 ...

  5. Clion+Cmake+Qt5+Qwt+msys2+MinGW在Windows下的安装配置使用教程

    摘要: CLion, a cross-platform C/C++ IDE. 本文主要介绍基于Clion作为IDE, MinGW作为编译器,CMake作为项目构建工具,开发基于Qt5.qwt的C++图 ...

  6. UICollectionView使用相关博文链接

    有关UICollectionView的几篇文章:1.UICollectionView简介及简单示例: http://puttin.github.io/blog/2013/04/08/a-simple- ...

  7. golang实现障碍、转弯最少的A*寻路

    目录 目标: 要点: 源码: 目标: 优先寻找无障碍的路径 目标不可达时,寻找障碍最少的路径 路径长度相等时,优先转弯最少的路径 多个目标点时,根据以上要求到达其中一个目标点即可 要点: 最优格子的选 ...

  8. dubbo+zookeeper报错:com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method

    com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method可能的错误原因有三个前两个是从网上摘得, 第三个是自己解决的 1.需要进行 ...

  9. 分享几个有意思的css js工具网站

    一.VOCABS(css html术语) vocabs 适合初学者快速认知各个代码的术语. 二.OverAPI(语言参考手册,几乎包含所有语言) OverAPI 适合快速查阅相关语言api 三.Jav ...

  10. SpringBoot报错:Table 'database_name.hibernate_sequence' doesn't exist

    引起条件: SpringBoot+JPA插入包含自增字段的对象 @Id @GeneratedValue private Integer id; 解决方法: 给注解添加属性 @Id @Generated ...