新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python程序:尋找數(shù)組中第二大元素
創(chuàng)新互聯(lián)Python教程:

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的蒙自網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
寫一個(gè) Python 程序來(lái)尋找 Numpy 數(shù)組中的第二大數(shù)字。我們使用 numpy 排序函數(shù)對(duì)數(shù)組進(jìn)行升序排序。接下來(lái),我們?cè)谧詈笠粋€(gè)索引位置打印該值。
import numpy as np
secLarr = np.array([11, 55, 99, 22, 7, 35, 70])
print("Array Items = ", secLarr)
secLarr.sort()
print("The Second Largest Item in this Array = ", secLarr[len(secLarr) - 2])
使用 For 循環(huán)范圍查找 numpy 數(shù)組中第二大數(shù)組的 Python 程序。
import numpy as np
secLarr = np.array([15, 22, 75, 99, 35, 70, 120, 60])
print("Array Items = ", secLarr)
first = second = min(secLarr)
for i in range(len(secLarr)):
if (secLarr[i] > first):
second = first
first = secLarr[i]
elif(secLarr[i] > second and secLarr[i] < first):
second = secLarr[i]
print("The Largest Item in this Array = ", first)
print("The Second Largest Item in this Array = ", second)
使用 for 循環(huán)范圍輸出的第二大 Python numpy 數(shù)組項(xiàng)
Array Items = [ 15 22 75 99 35 70 120 60]
The Largest Item in this Array = 120
The Second Largest Item in this Array = 99 網(wǎng)站標(biāo)題:Python程序:尋找數(shù)組中第二大元素
文章位置:http://m.5511xx.com/article/djhsjei.html


咨詢
建站咨詢
