average?

Mark McEahern marklists at mceahern.com
Fri Sep 20 23:40:22 EDT 2002


[Anton Vredegoor]
> Just a few more posters and we will get this function right, I am sure
>  >>> def average(seq):
>          return reduce(operator.add,seq)/float(len(seq))
                                           ^^^^^

Aw, why go through that trouble when you can just do this:

$ python
Python 2.2.1 (#1, Jun 25 2002, 10:55:46)
[GCC 2.95.3-5 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from __future__ import division
>>> import operator
>>> def average(seq):
...     return reduce(operator.add, seq) / len(seq)
...
>>> l = range(1, 5)
>>> a = average(l)
>>> print a
2.5
>>> print l
[1, 2, 3, 4]
>>>

<running and ducking for cover>

// m




More information about the Python-list mailing list