十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
#!/usr/bin/python
公司主营业务:成都网站设计、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联推出贵南免费做网站回馈大家。
# -*- coding:utf-8 -*-
# @Time : 2018/6/10 15:00
# @File : Sum_List.py
"""
输入一个2维列表 用该函数计算每一列的和并返回一个列表
"""
def sum_each_colum(olist):
"""
计算多维数字数组每列的和,并作为一个列表的元素,最后返回这个列表
:param olist:
:return:slist
"""
slist = []
if isinstance(olist, list):
slen = len(olist[0])
for i in range(slen):
s = 0.0
for temp in olist:
if isinstance(temp[i], (int, float)):
s += temp[i]
else:
return u'不是数字,无法求和'
else:
slist.append(s)
else:
return slist
else:
return u'输入与要求不符'
if __name__ == '__main__':
alist = eval(raw_input(u'请输入一个多维数字的列表[[1,2],[3,4]]:'))
print sum_each_colum(alist)
[1] 以 1 作为下标开始位置,即列表中首个元素为奇数位置,则代码如下:
代码一
def oddsum(x):
s = 0
for i in x[::2]:
s += i
return s
运行结果一
[2] 以 0 作为下标开始位置,即列表中首个元素为偶数位置,则代码如下:
代码二
def oddsum(x):
s = 0
for i in x[1::2]:
s += i
return s
运行结果二
def func(ls):
avg = sum(ls) / len(ls)
return [i-avg for i in ls]
# 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