sum

Subset sum - Recover solution

倾然丶 夕夏残阳落幕 提交于 2021-01-28 09:02:58
问题 I have written a dynamic programming algorithm that finds the total amount of subsets that sum up to a target value. However, I am having trouble developing a function to recover the solution (that is, print out the actual subsets). For example, let's take the set [1,3,5,7,9,10] with the target 13 . My algorithm calculates that there are 3 subsets. The output table is shown in the image below. Because this is a simple set, I can manually determine which three subsets make up the target. That

Sum of objects' prices in Django template

自闭症网瘾萝莉.ら 提交于 2021-01-28 08:18:46
问题 I would like to compute the total of a shopping cart in my template: This is my template with a table of products. I tried to use a Generator expression in my cart method but it doesn't work. Any thoughts? cart.html <table> {% if not cart_list %} {{ "The cart is empty" }} {% else %} <tr> <th>Name</th> <th>Price</th> </tr> {% for product in cart_list %} <tr> <td>{{ product.name }}</td> <td>${{ product.price }}</td> </tr> {% endfor %} <tr> <td>{{ Total }}</td> <td>{{ total_prices }}</td> </tr>

Row-wise sum for columns with certain names

感情迁移 提交于 2021-01-28 06:34:33
问题 I have a sample data: SampleID a b d f ca k l cb 1 0.1 2 1 2 7 1 4 3 2 0.2 3 2 3 4 2 5 5 3 0.5 4 3 6 1 3 9 2 I need to find row-wise sum of columns which have something common in names, e.g. row-wise sum(a, ca) or row-wise sum(b,cb) . The problem is that i have large data.frame and ideally i would be able to write what is common in column header, so that code would pick only those columns to sum Thank you beforehand for any assistance. 回答1: We can select the columns that have 'a' with grep ,

How to group subarray data using multiple column values and individually sum the remaining column values?

ぃ、小莉子 提交于 2021-01-28 06:03:20
问题 How can I group by Source and date then SUM Pageviews and revenue this array below Array ( [0] => Array ( [Source] => Analytics [Date] => 2017-10-31 [Source] => Google [visits] => 6000 [Pageviews] => 12,214 [Revenue] => 25 ) [1] => Array ( [Source] => Analytics [Date] => 2017-10-31 [Source] => Google [visits] => 600 [Pageviews] => 1015 [Revenue] => 10 ) [2] => Array ( [Source] => Analytics [Date] => 2017-10-31 [Source] => Facebook [visits] => 600 [Pageviews] => 1144 [Revenue] => 40 ) [3] =>

Sum second value in tuple for each given first value in tuples using Python

青春壹個敷衍的年華 提交于 2021-01-27 18:26:23
问题 I'm working with a large set of records and need to sum a given field for each customer account to reach an overall account balance. While I can probably put the data in any reasonable form, I figured the easiest would be a list of tuples (cust_id,balance_contribution) as I process through each record. After the round of processing, I'd like to add up the second item for each cust_id, and I am trying to do it without looping though the data thousands of time. As an example, the input data

Sum of numbers in an array with javascript

非 Y 不嫁゛ 提交于 2021-01-27 17:31:32
问题 I am trying to get the sum of all the numbers in an array. I am trying to do it in the most simple way but the sum display NAN. why is this so? please help var numbers = [45, 34, 12, 10, 8, 9]; var i; for(i=0 ; i<numbers.length; i++){ var sum = sum + numbers[i]; //alert(sum); } document.getElementById("demo").innerHTML="The sum is" + sum; <h2>JavaScript</h2> <p>This example finds the sum of all numbers in an array:</p> <p id="demo"></p> 回答1: var sum = [1, 2, 3].reduce(add, 0); function add(a,

Pandas - append column with sum of row values (if sum is even), or NaN (if odd)

让人想犯罪 __ 提交于 2021-01-27 16:42:43
问题 I have a dataframe and I want a new column t with sum of other columns in the same row. Criteria is I want NaN if the sum is odd, and the sum if the value is even. df = pd.DataFrame([[1, 8, 7, 2], [8, 5, 9, 4], [1, -5, 3, -2]], columns=list('pqrs')) df p q r s 0 1 8 7 2 1 8 5 9 4 2 1 -5 3 -2 Expected output: p q r s t 0 1 8 7 2 18.0 1 8 5 9 4 26.0 2 1 -5 3 -2 NaN 回答1: Using np.where : df['new'] = np.where(df.sum(1)%2==0, df.sum(1), np.nan) df p q r s new 0 1 8 7 2 18.0 1 8 5 9 4 26.0 2 1 -5 3

Pandas - append column with sum of row values (if sum is even), or NaN (if odd)

被刻印的时光 ゝ 提交于 2021-01-27 16:37:19
问题 I have a dataframe and I want a new column t with sum of other columns in the same row. Criteria is I want NaN if the sum is odd, and the sum if the value is even. df = pd.DataFrame([[1, 8, 7, 2], [8, 5, 9, 4], [1, -5, 3, -2]], columns=list('pqrs')) df p q r s 0 1 8 7 2 1 8 5 9 4 2 1 -5 3 -2 Expected output: p q r s t 0 1 8 7 2 18.0 1 8 5 9 4 26.0 2 1 -5 3 -2 NaN 回答1: Using np.where : df['new'] = np.where(df.sum(1)%2==0, df.sum(1), np.nan) df p q r s new 0 1 8 7 2 18.0 1 8 5 9 4 26.0 2 1 -5 3

Add unique groups to DF for each row including sum from other columns

亡梦爱人 提交于 2021-01-27 12:13:08
问题 I got a DatFrame looking like this: ID field_1 area_1 field_2 area_2 field_3 area_3 field_4 area_4 1 scoccer 500 basketball 200 swimming 100 basketball 50 2 volleyball 100 np.nan np.nan np.nan np.nan np.nan np.nan 3 basketball 1000 football 10 np.nan np.nan np.nan np.nan 4 swimming 280 swimming 200 basketball 320 np.nan np.nan 5 volleyball 110 football 160 volleyball 30 np.nan np.nan The original DataFrame has the same structure but containing columns field_1 up to field_30 as well as area_1

Add unique groups to DF for each row including sum from other columns

谁都会走 提交于 2021-01-27 12:05:08
问题 I got a DatFrame looking like this: ID field_1 area_1 field_2 area_2 field_3 area_3 field_4 area_4 1 scoccer 500 basketball 200 swimming 100 basketball 50 2 volleyball 100 np.nan np.nan np.nan np.nan np.nan np.nan 3 basketball 1000 football 10 np.nan np.nan np.nan np.nan 4 swimming 280 swimming 200 basketball 320 np.nan np.nan 5 volleyball 110 football 160 volleyball 30 np.nan np.nan The original DataFrame has the same structure but containing columns field_1 up to field_30 as well as area_1