创新互联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 示例中,我们使用 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
分享名称:Python程序:计算数组中偶数和奇数之和
网页网址:http://www.shufengxianlan.com/qtweb/news12/124812.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联