首页 > 编程知识 正文

Python 程序:寻找列表中最小数字

时间:2023-11-24 15:33:42 阅读:308922 作者:EFLU

写一个 Python 程序,用一个实际例子找到列表中最小的数字。

寻找列表中最小数字的 Python 程序示例 1

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

# Python Program to find Smallest Number in a List 

a = [10, 50, 60, 80, 20, 15]

print("The Smallest Element in this List is : ", min(a))

Python 最小列表号输出

The Smallest Element in this List is : 10

寻找列表中最小数字的 Python 程序示例 2

这个 python 程序同上。但是这次,我们允许用户输入列表的长度。接下来,我们使用 For Loop 给 Python 列表添加数字。

NumList = []
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))
    NumList.append(value)

print("The Smallest Element in this List is : ", min(NumList))

查找列表中最小数字的程序示例 3

Python 排序函数按照升序对列表元素进行排序。接下来,我们使用索引位置 0 来打印列表中的第一个元素。

a = [100, 50, 60, 80, 20, 15]

a.sort()
print("The Smallest Element in this List is : ", a[0])

Python 最小列表号使用排序函数输出

The Smallest Element in this List is : 20

寻找列表中最小数字的 Python 程序示例 4

这个 Python 列表最小的数字和上面一样。但是这次,我们允许用户输入他们自己的列表项。

NumList = []
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))
    NumList.append(value)

NumList.sort()
print("The Smallest Element in this List is : ", NumList[0])

Python 最小列表号输出

Please enter the Total Number of List Elements: 6
Please enter the Value of 1 Element : 7
Please enter the Value of 2 Element : 9
Please enter the Value of 3 Element : 22
Please enter the Value of 4 Element : 90
Please enter the Value of 5 Element : 5
Please enter the Value of 6 Element : 67
The Smallest Element in this List is :  5

返回列表中最小数字的 Python 程序示例 5

在这个程序中,我们没有使用任何内置函数,比如 sort,或者 min 函数。

NumList = []
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))
    NumList.append(value)

smallest = NumList[0]    
for j in range(1, Number):
    if(smallest > NumList[j]):
        smallest = NumList[j]
        position = j

print("The Smallest Element in this List is : ", smallest)
print("The Index position of the Smallest Element is : ", position)

Python 列表输出中最小的数字

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 223
Please enter the Value of 2 Element : 43
Please enter the Value of 3 Element : 22
Please enter the Value of 4 Element : 67
Please enter the Value of 5 Element : 54
The Smallest Element in this List is :  22
The Index position of the Smallest Element is :  2

从上面的 Python 程序中查找列表示例中的最小数字,用户插入的值是 NumList[5] = {223,43,22,67,54 } minist = NumList[0]= 223

第一次迭代–对于范围(1,5)中的 1–条件为真 因此,它开始在循环内执行 If 语句,直到条件失败。

如果 for 循环内的(最小> NumList[j])为真,因为(223 > 43) 最小= NumList[1] 最小= 43 位置= 1

第二次迭代:对于范围(1,5)中的 2–条件为真 If(最小>NumList[2])=(43>22)–条件为真 最小= NumList[2] 最小= 22 位置= 2T7】

第三次迭代:对于范围(1,5)中的 3–条件为真 如果(最小>NumList【3】)=(22>67)–条件为假 最小= 22 位置= 2

第四次迭代:对于范围(1,5)中的 4–条件为真 如果(22>54)–条件为假 最小= 22 位置= 2

第五次迭代:对于范围(1,5)中的 5–条件为假 因此,它退出循环。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。