python - 平均定义 - Python
问题描述
我有一个由里面的小列表组成的列表。每个小列表由 15 个元素组成,因此这个平均定义有效。但是,如果我将每个小列表更改为具有不同数量的元素,则此代码显然不起作用,我如何更改它以使其无论每个小列表中有多少元素都可以工作?谢谢
def avelist(inputlist):
total = 0
for row in inputlist:
total += sum(row)
return total/ (15* len(inputlist)
解决方案
只需跟踪项目的数量:
def avelist(inputlist):
total = 0
items = 0
for row in inputlist:
total += sum(row)
items += len(row)
return total / items
推荐阅读
- javascript - 如何在 Angular js(如 j Query)中单击按钮时提交带有本机操作 URL 的表单
- python - txt 文件不包含结果?
- angular - Angular 5 common.js: 263 Uncaught ReferenceError: $ st is not defined
- react-native - Flatlist 让我的应用程序变得很慢
- json - 像一个仪表板一样滚动小部件 - FLUTTER
- python - 同一图中的重尾分布和正态分布
- mysql - mySQL - 如果备用数据在列中,如何求和
- angular - 禁用角度 ng-select 窗口
- javascript - Angular 6 - 无法在画布上动态添加文本
- objective-c - 时区休息一小时