创新互联Python教程:
创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、成都网站制作、成都外贸网站建设、龙岩网络推广、微信小程序开发、龙岩网络营销、龙岩企业策划、龙岩品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联为所有大学生创业者提供龙岩建站搭建服务,24小时服务热线:028-86922220,官方网址:www.cdcxhl.com
写一个 Python 程序来计算一个数组或 Numpy 元素数组的平均值。Python Numpy 模块有求和和求平均值的方法来求数组项的求和和求平均值。
import numpy as np
arr = np.array([10, 20, 40, 60, 80, 100])
total = arr.sum()
avg = np.average(arr)
print('\nThe Sum Of Array Elements = ', total)
print('\nThe Average Of Array Elements = ', avg)
这个 python 程序使用 for 循环来迭代 Numpy Array 项,并计算所有元素的总和和平均值。
import numpy as np
arr = np.array([14, 25, 35, 67, 89, 11, 99])
total = 0
for i in range(len(arr)):
total = total + arr[i]
avg = total / len(arr)
print('\nThe Sum Of Array Elements = ', total)
print('\nThe Average Of Array Elements = ', avg)
The Sum Of Array Elements = 340
The Average Of Array Elements = 48.57142857142857
import numpy as np
arr = np.random.randint(10, 150, size = 11)
print('The Random Array Genegrated')
print(arr)
total = 0
i = 0
while i < len(arr):
total = total + arr[i]
i = i + 1
avg = total / len(arr)
print('\nThe Sum Of Array Elements = ', total)
print('\nThe Average Of Array Elements = ', avg)
The Random Array Genegrated
[123 91 119 131 45 121 48 53 44 60 82]
The Sum Of Array Elements = 917
The Average Of Array Elements = 83.36363636363636
这个 Python 示例允许输入数组元素,并找到所有数组项的总和和平均值。
import numpy as np
arrlist = []
Number = int(input("Total Array Elements to enter = "))
for i in range(1, Number + 1):
value = int(input("Please enter the %d Array Value = " %i))
arrlist.append(value)
arr = np.array(arrlist)
total = 0
for i in range(len(arr)):
total = total + arr[i]
avg = total / len(arr)
print('\nThe Sum Of Array Elements = ', total)
print('\nThe Average Of Array Elements = ', avg)
Total Array Elements to enter = 7
Please enter the 1 Array Value = 22
Please enter the 2 Array Value = 44
Please enter the 3 Array Value = 66
Please enter the 4 Array Value = 88
Please enter the 5 Array Value = 99
Please enter the 6 Array Value = 122
Please enter the 7 Array Value = 154
The Sum Of Array Elements = 595
The Average Of Array Elements = 85.0
新闻标题:Python程序:计算数组平均值
网站网址:http://www.shufengxianlan.com/qtweb/news31/540181.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联