您的位置:

Python 程序:计算数组中偶数和奇数之和

编写一个 Python 程序,使用 for 循环范围查找数组中偶数和奇数的和。if 条件(if(evenoddsumerr[I]% 2 = = 0))检查数组项是否可被二整除。如果为真,我们将该数组项添加到 evnArrSum。否则将该数组值添加到 oddArrSum。

# Sum of Even and Odd in Array

import numpy as np

evenOddSumArr = np.array([10, 25, 199, 450, 70, 111, 190, 7])
evenArrSum = 0
oddArrSum = 0

for i in range(len(evenOddSumArr)):
    if (evenOddSumArr[i] % 2 == 0):
        evenArrSum = evenArrSum + evenOddSumArr[i]
    else:
        oddArrSum = oddArrSum + evenOddSumArr[i]

print("The Sum of Even Numbers in evenOddSumArr Array = ", evenArrSum)
print("The Sum of Odd Numbers in evenOddSumArr Array  = ", oddArrSum)

寻找数组中偶数和奇数之和的 Python 程序

在这个 Python 示例中,我们使用 for 循环来迭代数组项。numpy mod 和余数函数检查每个数组项的余数是否等于零。

# Sum of Even and Odd in Array

import numpy as np

evenOddSumArr = np.array([10, 199, 14, 450, 170, 191, 200, 700])
evenArrSum = evenArrSum1 = evenArrSum2 = 0
oddArrSum = oddArrSum1 = oddArrSum2 = 0

for i in evenOddSumArr:
    if (i % 2 == 0):
        evenArrSum = evenArrSum + i
    else:
        oddArrSum = oddArrSum + i

print("The Sum of Even Numbers in evenOddSumArr Array = ", evenArrSum)
print("The Sum of Odd Numbers in evenOddSumArr Array  = ", oddArrSum)

print("\n=== Using mod function===")
for i in evenOddSumArr:
    if (np.mod(i, 2) == 0):
        evenArrSum1 = evenArrSum1 + i
    else:
        oddArrSum1 = oddArrSum1 + i

print("The Sum of Even Numbers in evenOddSumArr Array = ", evenArrSum1)
print("The Sum of Odd Numbers in evenOddSumArr Array  = ", oddArrSum1)

print("\n=== Using remainder function===")
for i in evenOddSumArr:
    if (np.remainder(i, 2) == 0):
        evenArrSum2 = evenArrSum2 + i
    else:
        oddArrSum2 = oddArrSum2 + i

print("The Sum of Even Numbers in evenOddSumArr Array = ", evenArrSum2)
print("The Sum of Odd Numbers in evenOddSumArr Array  = ", oddArrSum2)

Python Numpy 数组输出中偶数和奇数的和

The Sum of Even Numbers in evenOddSumArr Array =  1544
The Sum of Odd Numbers in evenOddSumArr Array  =  390

=== Using mod function===
The Sum of Even Numbers in evenOddSumArr Array =  1544
The Sum of Odd Numbers in evenOddSumArr Array  =  390

=== Using remainder function===
The Sum of Even Numbers in evenOddSumArr Array =  1544
The Sum of Odd Numbers in evenOddSumArr Array  =  390

使用 While 循环计算数组中偶数和奇数之和的 Python 程序。

# Sum of Even and Odd in Array

import numpy as np

evenOddSumArr = np.array([10, 25, 40, 75, 100, 125, 1100, 175])
i = 0
evenArrSum = evenArrSum1 = evenArrSum2 = 0
oddArrSum = oddArrSum1 = oddArrSum2 = 0

while (i < len(evenOddSumArr)):
    if (np.equal(evenOddSumArr[i] % 2, 0)):
        evenArrSum = evenArrSum + evenOddSumArr[i]
    else:
        oddArrSum = oddArrSum + evenOddSumArr[i]
    i = i + 1

print("The Sum of Even Numbers in evenOddSumArr Array = ", evenArrSum)
print("The Sum of Odd Numbers in evenOddSumArr Array  = ", oddArrSum)

Python Numpy 数组偶数和奇数之和使用 while 循环输出

The Sum of Even Numbers in evenOddSumArr Array =  1250
The Sum of Odd Numbers in evenOddSumArr Array  =  400

在这个 Python numpy 数组示例中,我们创建了一个(SumEvenOddNumbers(evenoddsumerr))函数,该函数返回偶数和奇数的和。

# Sum of Even and Odd in Array

import numpy as np

def SumEvenOddNumbers(evenOddSumArr):
    evenArrSum = 0
    oddArrSum = 0
    for i in evenOddSumArr:
        if (np.remainder(i, 2) == 0):
            evenArrSum = evenArrSum + i
        else:
            oddArrSum = oddArrSum + i

    return evenArrSum, oddArrSum

evenOddSumArr = np.array([10, 20, 25, 55, 100, 85, 200])
evensum, oddsum = SumEvenOddNumbers(evenOddSumArr)

print("The Sum of Even Numbers in evenOddSumArr Array = ", evensum)
print("The Sum of Odd Numbers in evenOddSumArr Array  = ", oddsum)

使用输出函数的 Python Numpy 数组奇偶和

The Sum of Even Numbers in evenOddSumArr Array =  330
The Sum of Odd Numbers in evenOddSumArr Array  =  165