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

這個(gè) Python 程序使用 for 循環(huán)范圍來打印 Numpy 數(shù)組中的奇數(shù)。if 語(yǔ)句(if (oddArr[i] % 2!= 0))檢查每個(gè)索引位置的 numpy 數(shù)組項(xiàng)不能被 2 整除。如果為真,(print(oddArr[i],end = ")打印奇數(shù)數(shù)組編號(hào)。
# 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 數(shù)組輸出中打印奇數(shù)
**The List of Odd Numbers in this oddArr Array***
25 65 75 121
使用 For 循環(huán)打印數(shù)組中奇數(shù)的 Python 程序
在這個(gè) Python 示例中,我們使用 numpy 余數(shù)和 numpy mod 函數(shù)來檢查每個(gè)可被 2 整除的數(shù)組項(xiàng)的余數(shù)是否不等于零。如果為真,則打印 numpy 數(shù)組中的奇數(shù)。
# 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 數(shù)組奇數(shù),用于循環(huán)輸出
**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 循環(huán)在 Numpy 數(shù)組中顯示奇數(shù)的 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 數(shù)組中打印奇數(shù)
**The List of Odd Numbers in this oddArr Array***
19 21 65 11 5
在這個(gè) Python numpy 數(shù)組的例子中,我們創(chuàng)建了一個(gè)函數(shù)來查找和打印奇數(shù)。
# 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) 新聞名稱:Python程序:打印數(shù)組中奇數(shù)
當(dāng)前網(wǎng)址:http://fisionsoft.com.cn/article/ccsdioo.html


咨詢
建站咨詢
