編寫一個 Python 程序,使用 for 循環範圍打印 Numpy 數組中的負數(對於 I in range(len(negarar))。if 條件(if(nega rar[I]< 0))發現 numpy 數組項小於零。如果為真,則打印該負數組項。
# Print Negatives in Array
import numpy as np
negaArr = np.array([11, -22, -33, 14, -17, 12, 0, -9, -34])
print("***The Negative Numbers in this negaArr Array***")
for i in range(len(negaArr)):
if (negaArr[i] < 0):
print(negaArr[i], end = " ")
***The Negative Numbers in this negaArr Array***
-22 -33 -17 -9 -34
使用 for 循環打印數組中負數的 Python 程序。
在這個 Python 示例中,for 循環(對於 negaArr 中的 num)迭代實際的 numpy 數組值。在第二個 for 循環中,numpy less 函數(if (np.less(i,0)= True))檢查 numpy 數組項是否小於零並返回 True。如果為真,則打印負數數組中的負數。
# Print Negatives in Array
import numpy as np
negaArr = np.array([1, -4, -9, 15, -22, 0, -99, 14, -10, -7, 6])
print("**The Negative Numbers in this negaArr Array***")
for num in negaArr:
if (num < 0):
print(num, end = " ")
print("\n\n=== Using less function===")
print("**The Negative Numbers in this negaArr Array***")
for i in negaArr:
if (np.less(i, 0) == True):
print(i, end = " ")
Python 程序使用 While 循環返回 Numpy 數組中的負數。
# Print Negative in Array
import numpy as np
negaArr = np.array([1, -34, -77, 11, -90, 88, 65, -17, -30])
i = 0
print("**The Negative Numbers in this negaArr Array***")
while (i < len(negaArr)):
if (np.less(negaArr[i], 0) == True):
print(negaArr[i], end = " ")
i = i + 1
**The Negative Numbers in this negaArr Array***
-34 -77 -90 -17 -30
在這個 Python numpy 數組的例子中,我們創建了一個(def print negatenumbers(negarar))函數來打印負數。
# Print Negative in Array
import numpy as np
def printNegativeNumbers(negaArr):
for i in negaArr:
if (np.less(i, 0) == True):
print(i, end = " ")
negaArr = np.array([16, -99, -88, 0, -77, 44, -55, -2, 19])
print("**The Negative Numbers in this negaArr Array***")
printNegativeNumbers(negaArr)
**The Negative Numbers in this negaArr Array***
-99 -88 -77 -55 -2
原創文章,作者:EKGU6,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/127868.html