用例程来写一个求平均值的算法
#!/usr/local/python/bin/python3 def FunCore(): total=0 counter=0 average=None tmp=yield average while True: total=total+tmp counter=counter+1 average=total/counter tmp=yield average if __name__ =="__main__": fc=FunCore() next(fc) for x in range(10): average=fc.send(x) print(average)
本文介绍了一个使用Python生成器实现的求平均值算法。通过不断接收新的输入并更新当前平均值,该算法能实时计算出一系列数值的平均值。
1万+

被折叠的 条评论
为什么被折叠?



