Python min List 函数

Python min List 是一个用于返回列表中最小值的列表函数。本节将通过实际示例讨论如何使用此列表最小函数。列表 min 函数的语法是:

min(list_name)

Python min List 函数示例

min 函数返回列表中的最小值。

IntList = [100, 150, 60, 80, 120, 105]
 
print("The Smallest Element in this List is : ", min(IntList))
The Smallest Element in this List is :  60

这是另一个关于整数列表的 min 函数示例。

int_list = [50, 10, 150, 20, 205, 500, 7, 25, 175]
print("List Items : ", int_list)

# Minimum list element
minimum = min(int_list)

print("\nThe Minimum item in this list = ", minimum)

最小或最小的列表项

List Items :  [50, 10, 150, 20, 205, 500, 7, 25, 175]

The Minimum item in this list =  7

列表最小示例 2

在此示例中,我们声明了一个字符串列表。接下来,我们使用 min 函数返回此列表中的最小值。在这里,它使用字母顺序查找列表中的最小值。

提示:请参阅 列表 文章和 列表方法 文章,以了解有关 Python 列表的所有信息。

Fruits = ['Orange', 'Banana', 'Kiwi', 'Apple', 'Grape', 'Blackberry']
 
print("The Smallest Element in this List is : ", min(Fruits))
The Smallest Element in this List is :  Apple

Python List min 示例 3

Python 程序 与第一个示例相同。但是,这次我们允许用户输入列表的长度。接下来,我们使用 For 循环 将数字添加到列表中。

intList = []

number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    value = int(input("Please enter the Value of %d Element : " %i))
    intList.append(value)
 
print("The Smallest Element in this List is : ", min(intList))
List min Function Example

列表最小示例 4

此程序允许用户输入自己的字符串或单词,然后找到最小字符串列表。

strList = []

number = int(input("Please enter the Total Number of List Elements: "))
for i in range(1, number + 1):
    item = input("Please enter the Value of %d Element : " %i)
    strList.append(item)
 
print("The Smallest Element in this List is : ", min(strList))
Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : kiwi
Please enter the Value of 2 Element : grape
Please enter the Value of 3 Element : apple
Please enter the Value of 4 Element : orange
The Smallest Element in this List is :  apple

列表 min 示例 5

我将此列表 min 函数用于混合列表。

MixedList = ['apple', 'Kiwi', 1, 5, 'Mango']
 
print("The Smallest Element in this List is : ", min(MixedList))

如您所见,它会引发错误,因为它无法在字符串和整数上应用 < 运算。

Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 3, in <module>
    print("The Smallest Element in this List is : ", min(MixedList))
TypeError: '<' not supported between instances of 'int' and 'str'
>>> 

列表 min 示例 6

这次,我们使用了嵌套列表(列表中的列表)。在这里,min 函数使用每个嵌套列表中的第一个值并返回最小或最小的值。

MixedList = [[1, 2], [2, 3], [4, 5]]
 
print("The Smallest Element in this List is : ", min(MixedList))
The Smallest Element in this List is :  [1, 2]