編寫一個 Python 程序來計算數組中的正數和負數,用於循環範圍。if 條件(if(posnegarar[I]> = 0))檢查數組項是否大於或等於零。如果為真,我們在正數組計數上加一;否則,將負數組計數加 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)
計算數組中正數和負數的 Python 程序
在這個 Python 示例中,我們使用 for 循環來迭代實際值,而不是索引位置。在第二個 for 循環中,numpy greater_equal 函數檢查特定 numpy 數組項是否大於或等於零並返回 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)
計算 Python Numpy 數組中的正數和負數,用於循環輸出
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 = 5
Python 程序使用 While 循環計算 Numpy 數組中的正數和負數。
# 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 循環輸出 計算正負 Python Numpy 數組項目
The Count of Positive Numbers in posNegArr Array = 3
The Count of Negative Numbers in posNegArr Array = 5
在這個 Python numpy 數組的例子中,我們創建了一個函數(countevenodnumbers(posNegArr)),返回正數和負數的計數。
# 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
原創文章,作者:W7O6Z,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/129195.html