日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

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

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

成都創(chuàng)新互聯(lián)提供成都做網(wǎng)站、成都網(wǎng)站建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì),成都品牌網(wǎng)站建設(shè)廣告投放平臺(tái)等致力于企業(yè)網(wǎng)站建設(shè)與公司網(wǎng)站制作,10年的網(wǎng)站開發(fā)和建站經(jīng)驗(yàn),助力企業(yè)信息化建設(shè),成功案例突破上千,是您實(shí)現(xiàn)網(wǎng)站建設(shè)的好選擇.

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

Python 程序在列表中查找第二大數(shù)字示例 1

這個(gè) python 程序允許用戶輸入長(zhǎng)度。接下來(lái),我們使用 For 循環(huán)在 Python 中向列表中添加數(shù)字。

python 中的排序函數(shù)以升序?qū)α斜碓剡M(jìn)行排序。接下來(lái),我們使用 Python 索引位置來(lái)打印列表中的最后一個(gè)元素。

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 - 2])

Python 程序在列表中查找第二大數(shù)字示例 2

這個(gè)程序按照升序?qū)υ剡M(jìn)行排序。接下來(lái),我們使用反轉(zhuǎn)功能來(lái)反轉(zhuǎn)列表項(xiàng)。最后,我們使用索引位置 1 來(lái)打印列表中的第二個(gè)元素。

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[1])
Please enter the Total Number of List Elements: 5
Please enter the Value of 1 Element : 20
Please enter the Value of 2 Element : 56
Please enter the Value of 3 Element : 78
Please enter the Value of 4 Element : 97
Please enter the Value of 5 Element : 60
The Largest Element in this List is :  78

Python 程序在列表中查找第二大數(shù)字示例 3

在這個(gè)程序中,我們沒(méi)有使用任何內(nèi)置功能,比如排序或者反轉(zhuǎn)功能。為此,我們使用For Loop

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)

first = second = NumList[0]
for j in range(1, Number):
    if(NumList[j] > first):
        second = first
        first = NumList[j]
    elif(NumList[j] > second and NumList[j] < first):
        second = NumList[j]

print("The Largest Element in this List is : ", first)
print("The Second Largest Element in this List is : ", second)
Please enter the Total Number of List Elements: 4
Please enter the Value of 1 Element : 55
Please enter the Value of 2 Element : 57
Please enter the Value of 3 Element : 22
Please enter the Value of 4 Element : 3
The Largest Element in this List is :  57
The Second Largest Element in this List is :  55

從上面的 Python 程序返回列表截圖中的第二大數(shù)字,可以觀察到用戶插入的值是

NumList[4] = {55,57,22,3} 第一個(gè)=第二個(gè)= NumList[0] = 55

第一次迭代–對(duì)于范圍(1,4)中的 1–條件為真 因此,它開始在循環(huán)內(nèi)執(zhí)行 If 語(yǔ)句,直到條件失敗。 如果 for 循環(huán)內(nèi)的(NumList[j] > first)為 True,因?yàn)?57>55) first = first = 55 first = NumList[1]= 57

第二次迭代:對(duì)于范圍(1,4)中的 2–條件為真 如果(NumList[2] >優(yōu)先)=(22>57)–條件為假。因此,它進(jìn)入 elif 語(yǔ)句 elif(NumList[2] >第二,NumList[2] <第一) elif(22 > 55 和 22<57)–條件為假

第三次迭代:對(duì)于范圍(1,4)中的 3–條件為真 如果(3>57)–條件為假 elif(3 > 55 和 3<57)–條件為假

第四次迭代:對(duì)于范圍(1,4)中的 4–條件為假。所以,它從循環(huán)中退出。


分享標(biāo)題:Python 程序:查找列表中的第二大數(shù)字
URL鏈接:http://www.dlmjj.cn/article/dppecho.html