简体   繁体   English

如何使用while循环确定列表中大于平均值的数量

[英]how to use while loop to determine the amount of numbers in a list that are greater the average

Hi I need to do this task in two ways: one way with for loop which I did and other with while loop but I don't secceed....The code I wrote is: 嗨,我需要以两种方式执行此任务:一种是使用for循环,另一种是使用while循环,但我没有剖析……。我编写的代码是:

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))

Your code is indeed unformatted. 您的代码确实未格式化。 Generally: 通常:

for x in some_list:
    ... # Do stuff

is equivalent to: 等效于:

i = 0
while i < len(some_list):
   ... # Do stuff with some_list[i]
   i += 1

The problem is that you are using while float in A > AV: in your code. 问题是您在代码while float in A > AV:中使用while float in A > AV:使用。 The while works until the condition is true. 在条件成立之前,while将起作用。 So as soon as it encounters some some number in list which is smaller than average, the loop exits. 因此,一旦列表中遇到一些小于平均数的数字,循环就会退出。 So, your code should be: 因此,您的代码应为:

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
i = 0
while i < len(A):
  if A[i] > AV: count += 1
  i += 1
print ("The number of elements bigger than the average is: " + str(count))

i hope it helped :) and i believe you know why i added another variable i . 我希望它能对您有所帮助:)并且我相信您知道为什么我要添加另一个变量i

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
i = 0
while i < len(A):
    if A[i] > AV:
        count += 1
    i += 1

print ("The number of elements bigger than the average is: " + str(count))

You can use something like the code below. 您可以使用类似下面的代码。 I have commented on each part, explaining the important parts. 我对每个部分都进行了评论,解释了重要的部分。 Note that your while float in A > AV is not valid in python. 请注意, while float in A > AV中的while float in A > AV在python中无效。 In your case, you should access elements of a list by indexing or using a for loop with the in keyword. 对于您的情况,应该通过索引或使用带有in关键字的for循环来访问列表的元素。

# In python, it is common to use lowercase variable names 
# unless you are using global variables
a = [5, 8, 4, 1, 2, 9]
avg = sum(a)/len(a)
print(avg)

gt_avg = sum([1 for i in a if i > avg])
print(gt_avg)

# Start at index 0 and set initial number of elements > average to 0
idx = 0
count = 0
# Go through each element in the list and if
# the value is greater than the average, add 1 to the count
while idx < len(a):
    if a[idx] > avg:
        count += 1
    idx += 1
print(count)

The code above will output the following: 上面的代码将输出以下内容:

4.833333333333333 4.833333333333333
3 3
3 3

Note: There are alternatives to the list comprehension that I've provided. 注意:我提供了列表理解的替代方法。 You could also use this piece of code. 您也可以使用这段代码。

gt_avg = 0
for val in a:
    if val > avg:
        gt_avg += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 取平均值并再次通过列表。 计算大于平均值的数量和小于平均值的数量 - Take the average and go through the list again. Count the amount of numbers greater than the average and the amount of numbers less than the average 计算列表中50或更大的所有数字的平均值? - Compute the average of all numbers in a list that are 50 or greater? 为什么 While 循环继续以及如何根据输入的数字计算平均值? - Why the While loop continues and how to calculate the average based on numbers input? 如何打印数字及其平均值的列表? - How to print a list of numbers and their average? 如何在python中使用while循环确定列表中有多少个字符串? - How to determine how many strings in a list using while loop in python? While循环,列表中的双精度数字 - While loop, double numbers in list 如何在不获得 2 个不同数字的情况下从敌人 HP 中减去 Randint 在 while 循环中生成的数量? - how can I subtract the amount Randint generates in the while loop from enemies HP without getting 2 different numbers? 如何从数字列表中获得平均“差价”? - How to get an average 'spread' from a list of numbers? 如何计算列表中 5 个数字的平均值? - How do I calculate the average of 5 numbers in a list? 如何使用 while 循环打印偶数 2 到 100? - How is it possible to use a while loop to print even numbers 2 through 100?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM