這個 Python 程序使用 for 循環範圍來打印 Numpy 數組中的奇數。if 語句(if (oddArr[i] % 2!= 0))檢查每個索引位置的 numpy 數組項不能被 2 整除。如果為真,(print(oddArr[i],end = “)打印奇數數組編號。
# Print Odd in Array
import numpy as np
oddArr = np.array([10, 25, 30, 65, 75, 50, 121])
print("**The List of Odd Numbers in this oddArr Array***")
for i in range(len(oddArr)):
if (oddArr[i] % 2 != 0):
print(oddArr[i], end = " ")
在 Python Numpy 數組輸出中打印奇數
**The List of Odd Numbers in this oddArr Array***
25 65 75 121
使用 For 循環打印數組中奇數的 Python 程序
在這個 Python 示例中,我們使用 numpy 餘數和 numpy mod 函數來檢查每個可被 2 整除的數組項的餘數是否不等於零。如果為真,則打印 numpy 數組中的奇數。
# Print Odd in Array
import numpy as np
oddArr = np.array([14, 23, 91, 18, 17, 89, 10])
print("**The List of Odd Numbers in this oddArr Array***")
for i in oddArr:
if (i % 2 != 0):
print(i, end = " ")
print("\n\n=== Using numpy mod function===")
print("**The List of Odd Numbers in this oddArr Array***")
for i in oddArr:
if (np.mod(i, 2) != 0):
print(i, end = " ")
print("\n\n=== Using numpy remainder function===")
print("**The List of Odd Numbers in this oddArr Array***")
for i in oddArr:
if (np.remainder(i, 2) != 0):
print(i, end = " ")
打印 Python Numpy 數組奇數,用於循環輸出
**The List of Odd Numbers in this oddArr Array***
23 91 17 89
=== Using numpy mod function===
**The List of Odd Numbers in this oddArr Array***
23 91 17 89
=== Using numpy remainder function===
**The List of Odd Numbers in this oddArr Array***
23 91 17 89
使用 While 循環在 Numpy 數組中顯示奇數的 Python 程序。
# Print Odd in Array
import numpy as np
oddArr = np.array([4, 19, 21, 88, 65, 16, 11, 10, 5])
i = 0
print("**The List of Odd Numbers in this oddArr Array***")
while (i < len(oddArr)):
if (np.not_equal(oddArr[i] % 2, 0)):
print(oddArr[i], end = " ")
i = i + 1
使用 while 輸出在 Python Numpy 數組中打印奇數
**The List of Odd Numbers in this oddArr Array***
19 21 65 11 5
在這個 Python numpy 數組的例子中,我們創建了一個函數來查找和打印奇數。
# Print Odd in Array
import numpy as np
def printOddNumbers(evenArr):
for i in oddArr:
if (np.remainder(i, 2) != 0):
print(i, end = " ")
oddArr = np.array([1, 5, 22, 17, 10, 11, 35, 44, 98])
print("**The List of odd Numbers in this oddArr Array***")
printOddNumbers(oddArr)
原創文章,作者:ECSK1,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/129856.html