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

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),明山企業(yè)網(wǎng)站建設(shè),明山品牌網(wǎng)站建設(shè),網(wǎng)站定制,明山網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,明山網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
編寫一個(gè) Python 程序,使用 for 循環(huán)范圍(或 i in range(len(posArr))打印數(shù)組中的正數(shù)。if 條件(if (posArr[i] >= 0))檢查 numpy 數(shù)組項(xiàng)是否大于或等于零。如果為真,則打印正數(shù)組項(xiàng)。
# Print Positives in Array
import numpy as np
posArr = np.array([10, -22, -14, 19, 11, -9, 0])
print("**The Positive Numbers in this posArr Array***")
for i in range(len(posArr)):
if (posArr[i] >= 0):
print(posArr[i], end = " ")
**The Positive Numbers in this posArr Array***
10 19 11 0
使用 for 循環(huán)打印數(shù)組中正數(shù)的 Python 程序
在這個(gè) Python 示例中,for 循環(huán)(posArr 中的 num)迭代原始值。在第二個(gè) for 循環(huán)中,numpy greater_equal 函數(shù)(if (np.greater_equal(i,0)= True))檢查 numpy 數(shù)組項(xiàng)是否大于或等于零并返回 True。如果為真,則打印 numpy 數(shù)組中的正數(shù)。
# Print Positive in Array
import numpy as np
posArr = np.array([1, 22, -99, -4, 14, 11, -10])
print("**The Positive Numbers in this posArr Array***")
for num in posArr:
if (num >= 0):
print(num, end = " ")
print("\n\n=== Using greater equal function===")
print("**The Positive Numbers in this posArr Array***")
for i in posArr:
if (np.greater_equal(i, 0) == True):
print(i, end = " ")
**The Positive Numbers in this posArr Array***
1 22 14 11
=== Using greater equal function===
**The Positive Numbers in this posArr Array***
1 22 14 11
Python 程序使用 while 循環(huán)返回 Numpy 數(shù)組中的正數(shù)。
# Print Positive in Array
import numpy as np
posArr = np.array([4, -5, 22, -9, -48, 11, 14])
i = 0
print("**The Positive Numbers in this posArr Array***")
while (i < len(posArr)):
if (np.greater_equal(posArr[i], 0) == True):
print(posArr[i], end = " ")
i = i + 1
**The Positive Numbers in this posArr Array***
4 22 11 14
在這個(gè) Python numpy 數(shù)組示例中,我們創(chuàng)建了一個(gè)(def printppositivennumbers(posArr))函數(shù)來(lái)打印正數(shù)。
# Print Positive in Array
import numpy as np
def printPositiveNumbers(posArr):
for i in posArr:
if (np.greater_equal(i, 0) == True):
print(i, end = " ")
posArr = np.array([1, -11, 0, 15, -9, -17, 22, -67, 55])
print("**The Positive Numbers in this posArr Array***")
printPositiveNumbers(posArr) 名稱欄目:Python程序:打印數(shù)組中正數(shù)
URL鏈接:http://www.dlmjj.cn/article/ccdedgs.html


咨詢
建站咨詢
