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

創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、龍巖網(wǎng)絡(luò)推廣、微信小程序開發(fā)、龍巖網(wǎng)絡(luò)營銷、龍巖企業(yè)策劃、龍巖品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供龍巖建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:www.cdcxhl.com
寫一個 Python 程序來計算一個數(shù)組或 Numpy 元素數(shù)組的平均值。Python Numpy 模塊有求和和求平均值的方法來求數(shù)組項(xiàng)的求和和求平均值。
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 循環(huán)來迭代 Numpy Array 項(xiàng),并計算所有元素的總和和平均值。
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
使用 while 循環(huán)計算數(shù)組平均值的 Python 程序
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 示例允許輸入數(shù)組元素,并找到所有數(shù)組項(xiàng)的總和和平均值。
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 網(wǎng)站標(biāo)題:Python程序:計算數(shù)組平均值
文章網(wǎng)址:http://m.5511xx.com/article/djsoojo.html


咨詢
建站咨詢
