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

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)站空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、金東網(wǎng)站維護(hù)、網(wǎng)站推廣。
編寫一個(gè) Python 程序來(lái)計(jì)算數(shù)組中的正數(shù)和負(fù)數(shù),用于循環(huán)范圍。if 條件(if(posnegarar[I]> = 0))檢查數(shù)組項(xiàng)是否大于或等于零。如果為真,我們?cè)谡龜?shù)組計(jì)數(shù)上加一;否則,將負(fù)數(shù)組計(jì)數(shù)加 1。
# Count Positive and Negative in Array
import numpy as np
posNegaArr = np.array([1, -19, -22, 16, -76, 0, 22, 50, -122, 70])
print("posNegaArr Array = ", posNegaArr)
posArrCount = 0
negArrCount = 0
for i in range(len(posNegaArr)):
if (posNegaArr[i] >= 0):
posArrCount = posArrCount + 1
else:
negArrCount = negArrCount + 1
print("The Count of Positive Numbers in posNegaArr Array = ", posArrCount)
print("The Count of Negative Numbers in posNegaArr Array = ", negArrCount)
計(jì)算數(shù)組中正數(shù)和負(fù)數(shù)的 Python 程序
在這個(gè) Python 示例中,我們使用 for 循環(huán)來(lái)迭代實(shí)際值,而不是索引位置。在第二個(gè) for 循環(huán)中,numpy greater_equal 函數(shù)檢查特定 numpy 數(shù)組項(xiàng)是否大于或等于零并返回 True。
# Count Positive and Negative in Array
import numpy as np
posNegaArr = np.array([4, 25, -9, 8, -6, -7, 0, 11, -17, 6, -44, 89])
posArrCount = posArrCount1 = 0
negaArrCount = negaArrCount1 = 0
for i in posNegaArr:
if (i >= 0):
posArrCount = posArrCount + 1
else:
negaArrCount = negaArrCount + 1
print("The Count of Positive Numbers in evennegaArr Array = ", posArrCount)
print("The Count of Negative Numbers in evennegaArr Array = ", negaArrCount)
print("\n=== Using greater_equal function===")
for i in posNegaArr:
if (np.greater_equal(i, 0) == True):
posArrCount1 = posArrCount1 + 1
else:
negaArrCount1 = negaArrCount1 + 1
print("The Count of Positive Numbers in evennegaArr Array = ", posArrCount1)
print("The Count of Negative Numbers in evennegaArr Array = ", negaArrCount1)計(jì)算 Python Numpy 數(shù)組中的正數(shù)和負(fù)數(shù),用于循環(huán)輸出
The Count of Positive Numbers in evennegaArr Array = 7
The Count of Negative Numbers in evennegaArr Array = 5
=== Using greater_equal function===
The Count of Positive Numbers in evennegaArr Array = 7
The Count of Negative Numbers in evennegaArr Array = 5Python 程序使用 While 循環(huán)計(jì)算 Numpy 數(shù)組中的正數(shù)和負(fù)數(shù)。
# Count Positive and Negative in Array
import numpy as np
posNegArr = np.array([418, -22, -33, 0, -89, -56, 11, -99])
i = 0
posArrCount = 0
negArrCount = 0
while (i < len(posNegArr)):
if (np.greater_equal(posNegArr[i], 0) == True):
posArrCount = posArrCount + 1
else:
negArrCount = negArrCount + 1
i = i + 1
print("The Count of Positive Numbers in posNegArr Array = ", posArrCount)
print("The Count of Negative Numbers in posNegArr Array = ", negArrCount)使用 while 循環(huán)輸出 計(jì)算正負(fù) Python Numpy 數(shù)組項(xiàng)目
The Count of Positive Numbers in posNegArr Array = 3
The Count of Negative Numbers in posNegArr Array = 5在這個(gè) Python numpy 數(shù)組的例子中,我們創(chuàng)建了一個(gè)函數(shù)(countevenodnumbers(posNegArr)),返回正數(shù)和負(fù)數(shù)的計(jì)數(shù)。
# Count Positive and Negative in Array
import numpy as np
def CountEvenOddNumbers(posNegArr):
posArrCount = 0
negArrCount = 0
for i in posNegArr:
if (np.greater_equal(i, 0) == True):
posArrCount = posArrCount + 1
else:
negArrCount = negArrCount + 1
return posArrCount, negArrCount
posNegArr = np.array([6, -9, -11, 8, 0, -14, -16, -9, 1])
positive, negative = CountEvenOddNumbers(posNegArr)
print("The Count of Positive Numbers in posNegArr Array = ", positive)
print("The Count of Negative Numbers in posNegArr Array = ", negative)The Count of Positive Numbers in posNegArr Array = 4
The Count of Negative Numbers in posNegArr Array = 5 網(wǎng)站題目:Python程序:統(tǒng)計(jì)數(shù)組中正數(shù)和負(fù)數(shù)
URL分享:http://www.dlmjj.cn/article/dpjipsj.html


咨詢
建站咨詢
