python - 如何使用while循环来确定列表中大于平均值的数字数量
问题描述
嗨,我需要以两种方式完成这项任务:一种方式使用 for 循环,另一种方式使用 while 循环,但我没有 secceed....我写的代码是:
A = [5,8,9,1,2,4]
AV = sum(A) / float(len(A))
count = 0
for i in A :
if i > AV :
count = count + 1
print ("The number of elements bigger than the average is: " + str(count))
count = 0
while float in A > AV:
count += 1
print ("The number of elements bigger than the average is: " + str(count))
解决方案
您的代码确实未格式化。一般来说:
for x in some_list:
... # Do stuff
相当于:
i = 0
while i < len(some_list):
... # Do stuff with some_list[i]
i += 1
推荐阅读
- php - 使用单独的服务器进行通知
- javascript - 如何使用 jquery 获取警报复选框值?
- 3d - 3d立体过渡预加载器
- .htaccess - 将网站 3 语言重定向到每个 url 的静态页面
- javascript - 在前端缓存动态生成的图像
- android - 使用正则表达式在没有 fontFamily 属性的 xml 中查找 TextViews
- codemirror - CodeMirror 将两个字符转换为单个 unicode 字符(例如 >= 到 ≥)
- mysql - 将 mysql 查询转换为 laravel 样式
- vue.js - 路由之间的延迟转换
- jmeter - 有没有办法控制jmeter中任何时间点处于活动状态的线程数