average?
Anton Vredegoor
anton at vredegoor.doge.nl
Fri Sep 20 21:00:31 EDT 2002
On Fri, 20 Sep 2002 15:10:47 -0400, Carl Banks <imbosol at vt.edu> wrote:
>Mark McEahern wrote:
>> def average(seq):
>> return reduce(operator.add(seq) / len(seq))
>> 1. Is there anything else dumb about the above?
>
>Well, yes; it seems to me you meant this:
>
> def average(seq):
> return reduce(operator.add,seq)/len(seq)
>> 2. Am I missing a builtin cousin of min, max that would do this for me?
>
>In pure Python, I don't think so. I would be surprised if the above
>function (when typed correctly) isn't the fastest way in pure Python.
Just a few more posters and we will get this function right, I am sure
...
>>> import operator
>>> def average(seq):
return reduce(operator.add,seq)/len(seq)
>>> average([1,2,3,4])
2
>>> def average(seq):
return reduce(operator.add,seq)/float(len(seq))
>>> average([1,2,3,4])
2.5
>>>
Anton.
More information about the Python-list
mailing list