日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:尋找列表中最大數(shù)字

創(chuàng)新互聯(lián)Python教程:

創(chuàng)新互聯(lián)公司是專業(yè)的桂林網(wǎng)站建設(shè)公司,桂林接單;提供做網(wǎng)站、網(wǎng)站設(shè)計,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行桂林網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!

寫一個 Python 程序,用一個實際例子找出列表中最大的數(shù)字。

Python 程序查找列表中最大的數(shù)字示例 1

Python max 函數(shù)返回列表中的最大值。

# Python Program to find Largest Number in a List 

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

print("The Largest Element in this List is : ", max(a))

Python 最大列表數(shù)輸出

The Largest Element in this List is : 120

Python 程序查找列表中最大的數(shù)字示例 2

這個 python 程序?qū)τ谧畲罅斜頂?shù)量的要求同上。但是這次,我們允許用戶輸入列表的長度。接下來,我們使用 For Loop 給 Python 列表添加數(shù)字。

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 Largest Element in this List is : ", max(NumList))

Python 程序查找列表中最大的數(shù)字示例 3

Python 排序函數(shù)按照升序?qū)α斜碓剡M行排序。接下來,我們使用索引位置打印列表中的最后一個元素。

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

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

Python 最大列表數(shù)輸出

The Largest Element in this List is :  80
>>> 

Python 程序查找列表中最大的數(shù)字示例 4

這個 Python 最大列表數(shù)程序同上。但這一次,我們允許用戶輸入自己的列表項。

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 Largest Element in this List is : ", NumList[Number - 1])

Python 最大列表數(shù)輸出

Please enter the Total Number of List Elements: 3
Please enter the Value of 1 Element : 90
Please enter the Value of 2 Element : 56
Please enter the Value of 3 Element : 70
The Largest Element in this List is :  90

在列表中查找最大數(shù)字的程序示例 5

該程序按照升序?qū)α斜眄椷M行排序。接下來,我們使用反轉(zhuǎn)功能來反轉(zhuǎn)列表項。最后,我們使用索引位置 0 來打印列表中的第一個元素。

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()
NumList.reverse()

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

Python 最大列表數(shù)輸出

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 60
Please enter the Value of 2 Element : 30
Please enter the Value of 3 Element : 90
Please enter the Value of 4 Element : 89
Please enter the Value of 5 Element : 45
The Largest Element in this List is :  90

返回列表中最大數(shù)字的 Python 程序示例 6

在這個程序中,我們沒有使用任何內(nèi)置功能,如排序、反轉(zhuǎn)或最大功能

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)

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

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

Python 最大列表數(shù)輸出

Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 70
Please enter the Value of 2 Element : 80
Please enter the Value of 3 Element : 120
Please enter the Value of 4 Element : 87
Please enter the Value of 5 Element : 46
The Largest Element in this List is :  120
The Index position of the Largest Element is :  2

用戶插入的值為 NumList[5] = {70,80,120,87,46} 最大= NumList[0] = 70

第一次迭代–對于范圍(1,5)中的 1–條件為真 因此,它開始在循環(huán)內(nèi)執(zhí)行 If 語句,直到條件失敗。

if(最大< NumList[j]) inside the for loop is True because (70 < 80) 最大= NumList[1] 最大= 80 位置= 1

第二次迭代:對于范圍(1,5)中的 2–條件為真 If(最大

第三次迭代:對于范圍(1,5)中的 3–條件為真 If(最大

第四次迭代:對于范圍(1,5)中的 4–條件為真 如果(最大

第五次迭代:對于范圍(1,5)中的 5–條件為假 因此,它退出循環(huán)。


本文標題:Python程序:尋找列表中最大數(shù)字
URL網(wǎng)址:http://m.5511xx.com/article/cddesop.html