新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python min()
內(nèi)置函數(shù)min()有助于返回給定 iterable 中的最小元素。也可以在兩個或多個參數(shù)之間找到最小的元素。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比旬陽網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式旬陽網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋旬陽地區(qū)。費用合理售后完善,十余年實體公司更值得信賴。
# to find the smallest item in an iterable
**min(iterable, *iterables, key, default)**
# to find the smallest item between two or more objects
**min(arg1, arg2, *args, key)**
最小()參數(shù):
帶有 iterable 的min()函數(shù)具有以下參數(shù)。
*分鐘(可重復(fù),可重復(fù),鍵,默認(rèn))**
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 可迭代的 | 諸如列表、元組、集合、字典等可條目。 | 需要 |
| *可重復(fù) | 任意數(shù)量的可滴定物;可以不止一個 | 可選擇的 |
| 鍵 | 傳遞數(shù)據(jù)項并進行比較的一種關(guān)鍵功能 | 可選擇的 |
| 系統(tǒng)默認(rèn)值 | 如果給定的 iterable 為空,則為默認(rèn)值 | 可選擇的 |
不帶iterable()的iterable()函數(shù)有以下參數(shù)。
【t0 min(arg1、arg2、*args、鍵)】的縮寫
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| arg1 | 一個對象;可以是數(shù)字、字符串等 | 需要 |
| arg2 | 一個對象;可以是數(shù)字、字符串等 | 需要 |
| *參數(shù) | 任意數(shù)量的對象 | 可選擇的 |
| 鍵 | 傳遞每個參數(shù)并進行比較的一個關(guān)鍵函數(shù) | 可選擇的 |
最小()返回值
在傳遞空迭代器的情況下,它會引發(fā) ValueError 異常。為了避免這種情況,我們可以傳遞默認(rèn)參數(shù)。 如果我們傳遞多個迭代器,那么從給定的迭代器中返回最小的項。
| 投入 | 返回值 | | 整數(shù) | 最小整數(shù) | | 線 | 具有最小 Unicode 值的字符 |
Python 中min()方法的示例
示例 1:獲取列表中最小的項目
number = [13, 2, 8, 25, 10, 46]
smallest_number = min(number);
print("The smallest number is:", smallest_number)
Output
輸出:
The smallest number is: 2示例 2:列表中最小的字符串
languages = ["Python", "C Programming", "Java", "JavaScript"]
smallest_string = min(languages);
print("The smallest string is:", smallest_string)
輸出:
The smallest string is: C Programming示例 3:在字典中查找min()
square = {2: 4, 3: 9, -1: 1, -2: 4}
# the smallest key
key1 = min(square)
print("The smallest key:", key1) # -2
# the key whose value is the smallest
key2 = min(square, key = lambda k: square[k])
print("The key with the smallest value:", key2) # -1
# getting the smallest value
print("The smallest value:", square[key2]) # 1
輸出:
TThe smallest key: -2
The key with the smallest value: -1
The smallest value: 1例 4:在給定的數(shù)字中找出最小值
result = min(4, -5, 23, 5)
print("The minimum number is:", result)
輸出:
The minimum number is -5示例 5:查找對象的最小值()
class Data:
id = 0
def __init__(self, i):
self.id = i
def __str__(self):
return 'Data[%s]' % self.id
def get_data_id(data):
return data.id
# min() with objects and key argument
list_objects = [Data(1), Data(2), Data(-10)]
print(min(list_objects, key=get_data_id))
輸出:
Data[-10] 網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:Python min()
文章鏈接:http://fisionsoft.com.cn/article/djdiiop.html


咨詢
建站咨詢
