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

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的蒙自網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
寫一個 Python 程序來尋找 Numpy 數(shù)組中的第二大數(shù)字。我們使用 numpy 排序函數(shù)對數(shù)組進(jìn)行升序排序。接下來,我們在最后一個索引位置打印該值。
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ù)組項
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 當(dāng)前文章:Python程序:尋找數(shù)組中第二大元素
網(wǎng)頁URL:http://www.5511xx.com/article/djhsjei.html


咨詢
建站咨詢
