Author Topic: Creating a function that finds the average of a list in python  (Read 1023 times)

0 Members and 1 Guest are viewing this topic.

Offline IrishYoga

  • Serf
  • *
  • Posts: 31
  • Cookies: -2
    • View Profile
Creating a function that finds the average of a list in python
« on: September 01, 2015, 08:24:55 pm »
Hello, I have this code that I think should be giving me the average of a list but when I put it into python, the error says error:division by zero. I'd appreciate it if you guys looked at this code and told me what i'm doing wrong.
Code: (python) [Select]
def f(x):
    d = []
    if x == d:
        return None
    else:
        sum = 0
        for n in d:
            sum = sum + n
        length = len(d)
    total = sum/length
    print(total)
   
« Last Edit: October 16, 2015, 01:56:14 pm by proxx »

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: Creating a function that finds the average of a list in python
« Reply #1 on: September 01, 2015, 08:37:44 pm »
Code: (python) [Select]
def f(x):
    d = []
    if x == d:
        return None
    else:
        sum = 0
        for n in d:
            sum = sum + n
        length = len(d)
    total = sum/length
    print(total)

Because "d" is an empty array, so length is zero. And you're dividing by "d"s length in line 10.

Also, this code can be improved quite a bit:
Line 2 is unneccessary
Line 3 could be changed to "if x:", as an array x evaluates to false if it is empty and to true if it isn't. Then you could put the rest of the code inside that "if".
Line 8 can be shortened to "sum += n".
Line 11 should be "return total", if you want to use the average for some other stuff.
Also, improve variable naming, to be more clear what a variable represents.
Also, "sum" is a built-in function, so don't use it as a variable name!

This would be the improved version:

Code: (python) [Select]
def calcAverage(_array):
    if _array:
        _sum = 0
        for i in _array:
            _sum += i
        average = _sum/len(_array)
        return average
    else:
        return None
« Last Edit: September 01, 2015, 09:37:15 pm by flowjob »
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline IrishYoga

  • Serf
  • *
  • Posts: 31
  • Cookies: -2
    • View Profile
Re: Creating a function that finds the average of a list in python
« Reply #2 on: September 01, 2015, 09:30:57 pm »
Thanks for not only putting the correct code but also pointing out what I did wrong! This really helped.

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: Creating a function that finds the average of a list in python
« Reply #3 on: September 01, 2015, 09:42:06 pm »
Also, I forgot to mention that if you enclose your code with the [ c o d e ] and [ / c o d e ]  tags it gets formatted to be easier to read. You can also specify a language for syntax-highlighting like this (e.g. python): [ c o d e = p y t h o n ]
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline novaccainne

  • Serf
  • *
  • Posts: 29
  • Cookies: 2
    • View Profile
Re: Creating a function that finds the average of a list in python
« Reply #4 on: October 06, 2015, 03:32:05 pm »
Just for fun :

Code: [Select]
if  type(myarray) is list and  len(myarray) > 0 :
sum(myarray)/len(myarray) == sum([x for x in myarray]) / len(myarray) == reduce(lambda x,y: x+y,myarray) / len(myarray)


http://caisbalderas.com/blog/iterating-with-python-lambdas/
http://www.python-course.eu/lambda.php

Offline fayesafe

  • /dev/null
  • *
  • Posts: 14
  • Cookies: 4
    • View Profile
Re: Creating a function that finds the average of a list in python
« Reply #5 on: October 16, 2015, 01:35:31 pm »
In Python 2, you could also use the builtin reduce()-function. (Consider functools.reduce() for Python 3.) It then looks like that:

Code: (python) [Select]
l = [1, 2, 3, 4, 5, 6, 7, 8]
average = reduce(lambda x, y: x + y, l) / float(len(l))
I reserve the right to be wrong. I reserve the right to change my mind.