Skip to content Skip to sidebar Skip to footer

Average -python

I am trying to find the average of numbers in ranges (i.e. find the average of all numbers in range 1-1000). I wrote the following code to do this, but the due to the if-statement,

Solution 1:

It is not due to the if statement. You simply put the print(..) in the if in the for loop. By moving it outside the outer for, it will print the average for the entire file:

defave_data(x, y):
    mylist = []  # move insidefor line in filename:    
        for number in line.split():
            ifint(number) > x andint(number) < y:
                mylist.append(int(number)) 
    print(sum(mylist)/len(mylist))  # for the entire file.

You also move the mylist variable better inside the function. Otherwise it is a global variable, and other functions can alter it. Furthermore a second run would take the average of the two files.

That being said, you make things too complicated. You can simply use:

defave_data(x, y):
    mylist = [int(number) for line in filename for number in line.split()
                          if x < int(number) < y]
    print(sum(mylist)/len(mylist))  # for the entire file.

Solution 2:

You can also use list-comprenehsion instead of for-loop, as the following:

mylist = [int(num) for line in filename for num in line.split() if y > int(num) > x]
printsum(mylist) / len(mylist)

You can also add a condition to check if the list is empty:

printsum(mylist) / len(mylist) if mylist else'No numbers in that range'

Solution 3:

You are seeing multiple numbers because your print statement is within your for loop. Take a look at this pseudocode:

create an empty list
open the file
foreach line in the file
    foreach number in the line
        if the number iswithin the rangeadd the number to the list
            compute the average of the list of numbers

You see, you are computing the average every time you find a number within the required range. Instead, what you want to do is to gather all the numbers first (within the for loop), and then (outside the for loop) compute the average:

create an empty list
open the file
foreach line in the file
    foreach number in the line
        if the number iswithin the rangeadd the number to the list
compute the average of the list of numbers

This is achieved with this code:

def ave_data(x, y):
    nums = []
    for line in filename:    
        for number in line.split():
            if int(number) > x and int(number) < y:
                nums.append(int(number)) 
    print(sum(mylist)/len(mylist))

Here's a slightly better way to do the same thing:

defave_data(x,y):
    total = 0
    nums = 0withopen('path/to/file') as infile:
        for line in infile:
            for num in line.split():
                num = int(num)
                ifnot (x < num < y): continue
                total += num
                nums += 1ifnot nums:
        print("There were no numbers within the required range")
        returnprint("The average is", total/nums)

Post a Comment for "Average -python"