Finding a sum in nested list using a lambda function
One approach is to use a generator expression: total = sum(int(v) for name,v in table)
One approach is to use a generator expression: total = sum(int(v) for name,v in table)
np.dot is the dot product of two matrices. |A B| . |E F| = |A*E+B*G A*F+B*H| |C D| |G H| |C*E+D*G C*F+D*H| Whereas np.multiply does an element-wise multiplication of two matrices. |A B| ⊙ |E F| = |A*E B*F| |C D| |G H| |C*G D*H| When used with np.sum, the result being equal is merely … Read more
Its a bit tricky – the sum() function takes the start and adds it to the next and so on You need to implement the __radd__ method: class T: def __init__(self,x): self.x = x def __radd__(self, other): return other + self.x test = (T(1),T(2),T(3),200) print sum(test)
If untyped (replace int with the correct data type): var sum = table.AsEnumerable().Sum(x=>x.Field<int>(3)); or: var sum = table.AsEnumerable().Sum(x=>x.Field<int>(“SomeProperty”)); If typed: var sum = table.Sum(x=>x.SomeProperty);
Note: My computer is running .Net 4.5 RC, so it’s possible that my results are affected by this. Measuring the time it takes to execute a method just once is usually not very useful. It can be easily dominated by things like JIT compilation, which are not actual bottlenecks in real code. Because of this, … Read more
Put it outside: SELECT COALESCE( ( SELECT SUM(i.Logged) FROM tbl_Sites s INNER JOIN tbl_Incidents i ON s.Location = i.Location WHERE s.Sites = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0) GROUP BY s.Sites ), 0) AS LoggedIncidents If you are returning multiple rows, change INNER JOIN to LEFT JOIN SELECT COALESCE(SUM(i.Logged),0) FROM tbl_Sites s … Read more
I don’t believe there is a way to get only the value. You could just do ${{ total_paid.amount__sum }} in your template. Or do total_paid = Payment.objects.all().aggregate(Sum(‘amount’)).get(‘amount__sum’, 0.00) in your view. EDIT As others have pointed out, .aggregate() will always return a dictionary with all of the keys from the aggregates present, so doing .get() … Read more
Select SUM(CASE When CPayment=”Cash” Then CAmount Else 0 End ) as CashPaymentAmount, SUM(CASE When CPayment=”Check” Then CAmount Else 0 End ) as CheckPaymentAmount from TableOrderPayment Where ( CPayment=”Cash” Or CPayment=”Check” ) AND CDate<=SYSDATETIME() and CStatus=”Active”;
You could try this: In [9]: l = [[3,7,2],[1,4,5],[9,8,7]] In [10]: [sum(i) for i in zip(*l)] Out[10]: [13, 19, 14] This uses a combination of zip and * to unpack the list and then zip the items according to their index. You then use a list comprehension to iterate through the groups of similar indices, … Read more