I am trying to write a Python code that reads each item in a list and add and find the average of that list with certain conditions

neo

So I'm trying to add all the numbers of a list and divide that sum by how many numbers in that list are not equal to 0. The code does not include the part where I summed all the numbers together. What I did in this part of my code is that I tried to check each individual item in a list one at a time. Eventually, it would tell me how many numbers were not equal to zero with the variable sum_div.

b=4
c=0
d=6
e=0

I_list = [ b, c, d, e]

sum_div = 0

ip = I_list[0]

for i in range(0, len(I_list), 1) :
    while ip != 0:
        sum_div += 1
    elif ip == 0:
        sum_div += 0
    
print(sum_div)

sum_div was supposed to equal 2 in this case.

Cory Kramer

You can use a list comprehension to filter out the elements that are 0 then determine the len of that list. Then just divide the sum by that length to compute the average (of non-zero elements)

>>> sum(I_list) / len([i for i in I_list if i])
5.0

Technically you could also guard against an empty list, or a list of all zeroes

try:
    average = sum(I_list) / len([i for i in I_list if i])
except ZeroDivisionError:
    average = whatever_here  # don't know what you'd want in this case

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I write a bash script that reads a list from a text file and takes user interaction for each item?

I am trying to find the maximum occurrences of a string in a list using Python

Python: How to find the average on each array in the list?

In VBscript I am getting a type mismatch error when trying to split each item in an array list into a single array

I am trying to get the average per object in a list

I am trying to programmatically add images to a list/array and access them in a for each loop to change their source image

I am trying to compare an item from a tuple and a list but get an error

I am trying to search for a specific item in linked list

I am trying to batch some lists according to each list's weight in Python

*i was trying to add elements in a linked list in python but i am getting TypeError: '>' not supported between instances of 'int' and 'NoneType

How can I find the average of each similar entry in a list of tuples?

I am trying to define a Linked list in Python, but why it is always going to add first element

how to calculate average for each item in a list with LINQ?

I am trying to list all guild members with python requests but I am getting an error: {'message': 'Missing Access', 'code': 50001}

Im trying to write a function to get an average out of a table in python but I get an error saying 'List index out of range'

Find data-id for each list item and add together

New to python and I'm trying to add an item to individual lists in a nested list

Find the average of every list item in the same position of the List<List<Double>>

Add an item for each list value

I am trying to add entries from list in list to dictionary present in other list

Python: Find an item in a list

how to add to a dictionary from a list with certain conditions

Trying to create for loop which iterates through each item in a list to find any matches in a separate list and then replace the match

I am trying to read a text file and turn it into a list of dictionaries Python

How do I add text to each item in a list

How can I add same text to each item of the list?

Trying to add a comma in between each value of my list python

How do I extract an item in a list if it has a certain string in python?

Find all possible combination in a python list skipping an item each time