On 05/11/16 12:07, Peter Otten wrote:
> sum_highest = lambda items, n: sum(sorted(items, reverse=True)[:max(n, 0)])
>
> or better:
>
> import heapq
>
> def sum_highest(items, n):
> return sum(heapq.nlargest(n, items))
No, the first solution is "better" because it used lambda
and slicing a
Lloyd Francis wrote:
> I want to write a function that will calculate and return the sum of the
> *n* highest value in a list *a. *Also, when n is less than 0, the answer
> should be zero, and if n is greater than the number of elements in the
> list, all elements should be included in the sum.
>
Interesting. Tracey Jones has a very similar question...
- Cameron Simpson
On 04Nov2016 16:44, Lloyd Francis wrote:
Hi,
I want to write a function that will calculate and return the sum of the *n*
highest value in a list *a. *Also, when n is less than 0, the answer should
be zero, and if n is
On 04/11/16 16:44, Lloyd Francis wrote:
It looks suspiciously like you posted this same message
from two addresses with different subjects, please don't
do that as it splits the responses and makes searching
archives more difficult.
> I want to write a function that will calculate and return the
Hi,
I want to write a function that will calculate and return the sum of the *n*
highest value in a list *a. *Also, when n is less than 0, the answer should
be zero, and if n is greater than the number of elements in the list, all
elements should be included in the sum.
In addition i want to writ