您的位置:

Python 程序:打印数组中奇数

这个 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)