python怎么保存数据的均值(python保存中间数据)

http://www.itjxue.com  2023-03-25 04:25  来源:未知  点击次数: 

python 问题 如何用python 求n个数的平均值 然后保留小数

# coding = GBK

a =[1,2,3,4,5]

sum=0

b = len(a)

print("这个数组的长度为:",b)

for i? in a:

sum =sum +i

print("这个数组之和为:",sum)

print("这个数组平均数为",sum/b)

import sys

sum = 0

cnt = 0

f = open('1.txt', 'r')

files = f.readline()

while (files ):

sum = sum + float(files .split(",")[0])

cnt = cnt + 1

files = f.readline()

print(sum / cnt)

f.close()

或者。

#!/usr/bin/env pythonimport timeimport numpy as np

dd = np.random.randint(0, 20, size=(2*1000*1000))t_start = time.clock()avg_sum1 =

0.0BlockOffset = 0 ? ? while BlockOffset len(dd):

if dd[BlockOffset + 1] = 10:

avg_sum1 += dd[BlockOffset + 1] * 0.1

else:

avg_sum1 += dd[BlockOffset + 0] * 0.01

BlockOffset += 2print('Avg: ' + str(avg_sum1 / len(dd) / 2)) ? ?print('Exe time: ' +

str(time.clock() - t_start))

扩展资料:

python 实现求和、计数、最大最小值、平均值、中位数、标准偏差、百分比。

import?sys

class?Stats:

def?__init__(self, sequence):

# sequence of numbers we will process

# convert all items to floats for numerical processing

self.sequence?=?[float(item)?for?item?in?sequence]

def?sum(self):

if?len(self.sequence) ?1:

return?None

else:

return?sum(self.sequence)

def?count(self):

return?len(self.sequence)

def?min(self):

if?len(self.sequence) ?1:

return?None

else:

return?min(self.sequence)

def?max(self):

if?len(self.sequence) ?1:

return?None

else:

return?max(self.sequence)

def?avg(self):

if?len(self.sequence) ?1:

return?None

else:

return?sum(self.sequence)?/?len(self.sequence)?

def?median(self):

if?len(self.sequence) ?1:

return?None

else:

self.sequence.sort()

return?self.sequence[len(self.sequence)?//?2]

def?stdev(self):

if?len(self.sequence) ?1:

return?None

else:

avg?=?self.avg()

sdsq?=?sum([(i?-?avg)?**?2?for?i?in?self.sequence])

stdev?=?(sdsq?/?(len(self.sequence)?-?1))?**?.5

return?stdev

def?percentile(self, percentile):

if?len(self.sequence) ?1:

value?=?None

elif?(percentile =?100):

sys.stderr.write('ERROR: percentile must be 100.? you supplied: %s\n'%?percentile)

value?=?None

else:

element_idx?=?int(len(self.sequence)?*?(percentile?/?100.0))

self.sequence.sort()

value?=?self.sequence[element_idx]

return?value

用python做一个程序,能够存储数据并输出平均值

这么简单实现行吗

sum?=?0.0

count?=?0

with?open('numbers.txt',?'a')?as?f:

????while?True:

????????value?=?input('Input?number:')

????????if?value?==?'':

????????????break

????????sum?=?sum?+?float(value)

????????count?+=?1

????????f.write(value?+?'\n')

????f.write('avg=?'?+?str(sum?/?count)?+?'\n')

????print('avg=?'?+?str(sum?/?count)?+?'\n')

python怎么求平均值及所有大于平均值的数?

题主你好,

代码及测试截图如下:

希望可以帮到题主, 欢迎追问

python怎么将学生的成绩保存成字典 并返回三门课的平均值

global students

students={

"Zhangsan":(89,88,80),

"Bill":(90,90,86),

"Tom":(80,98,85),

"Jack":(79,90,89)}

def average(*shu):

(Tab缩进)AVE=sum(shu)/len(shu)

(Tab缩进)return AVE

def average_courses(name):

(Tab缩进)for k,v in students.items():

(Tab缩进)(Tab缩进)if k==name:

(Tab缩进)(Tab缩进)(Tab缩进)return [name, v, round(average(*v), 1)]

if __name__ == "__main__":

(Tab缩进)#print(average_courses("Tom"))

(Tab缩进)#print(average_courses("Tom")[2])

(Tab缩进)for k in students.keys():

(Tab缩进)(Tab缩进)print([k,average_courses(k)[2]])

'''python运行效果

['Zhangsan', 85.7]

['Bill', 88.7]

['Tom'

(责任编辑:IT教学网)

更多

推荐windows vista文章