Add Sum of Values of Two Lists into New List

Add SUM of values of two LISTS into new LIST

The zip function is useful here, used with a list comprehension.

[x + y for x, y in zip(first, second)]

If you have a list of lists (instead of just two lists):

lists_of_lists = [[1, 2, 3], [4, 5, 6]]
[sum(x) for x in zip(*lists_of_lists)]
# -> [5, 7, 9]

How to sum the elements of 2 lists in python?

you can use zip/map:

result = list(map(sum,zip(list1,list2)))

Alternative, via list_comprehension:

result = [i+j for i,j in zip(list1,list2)]

OUTPUT:

[11, 13, 15, 17, 19]

Sum values in two arrays lists and return a third list

You can do the following (given that you cannot upgrade to Java 8)

  • Create a Map with stock code as key and stock as the value
Map<Integer, Stock> netStockMap = new HashMap<Integer, Stock>();
  • Then add all the old stock entries to that map as follows.
for (Stock oldStockItem: oldStock) {
netStockMap.put(oldStockItem.getStockCode(), oldStockItem);
}
  • Then for each new stock entry, check if an old entry exists in the map. If exists, create a new stock entry combining both old and new entry. Otherwise just add the new stock entry to the map.
for (Stock newStockItem: newStock) {
Integer stockCode = newStockItem.getStockCode();
Stock oldStockItem = netStockMap.get(stockCode);
if (oldStockItem != null) {
Stock netStockItem = new Stock(stockCode, oldStockItem.getStockQuantity() + newStockItem.getStockQuantity(),oldStockItem.getStockValue() + newStockItem.getStockValue());
netStockMap.put(stockCode, netStockItem);

} else {
netStockMap.put(stockCode, newStockItem);
}
}
  • Then get the values of the map as the final stock list
List<Stock> netStock = new ArrayList<Stock>(netStockMap.values());

Given 3 lists, find which two elements in the first two lists sum as close as possible to each value in the third list

Your current solution is O(n^2 log(n)) time, and O(n^2) memory. The reason is that your ordered is a list of size n^2 that you then sort, and do lots and lots of binary searches on. This gives you much poorer constants, and a chance of going into swap.

In you case of 10,000 each, you have a dictionary with 100,000,000 keys, that you then sort, and walk through. Which is billions of operations and GB of data. If your machine winds up in swap, those operations will slow down a lot and you have a problem.

I would suggest that you sort lists 2 and 3. For each l1 in list 1 it lets you walk through l1+l2 in parallel with walking through l3, finding the best in l3. Here that is in pseudo-code:

record best for every element in list 3 to be list1[1] + list2[1]
foreach l1 in list 1:
start l2 at start of list2
start l3 at start of list3
while can advance in both list 2 and list 3:
if advancing in list2 improves l1 + l2 as approx of l3:
advance in list 2
else:
if l1 + l2 is better than best recorded approx of l3:
record l1 + l2 as best for l3
advance in list 3
while can advance in list 3:
if l1 + l2 is better than best recorded approx of l3:
record l1 + l2 as best for l3
advance in list 3
if l1 + l2 is better than best recorded approx of l3:
record l1 + l2 as best for l3

This requires sorted versions of list2 and list3, and a lookup from list3 to best approximation. In your example of 10,000 items each, you have 2 data structures of size 10,000, and have to do roughly 200,000,000 operations. Better than billions and no problems with pressing against memory limits.

Add or sum a value to list till condition is not valid anymore and create a new element when condition is valid Python

You were appending in the b and c array each time. You need to add 0.04 to that index until your condition is met.

import numpy as np
a= [1,2,3,0,0,6,7,8,0,0,9,10,0,0]
a = np.array(a)
b=[]
c =[]
value1 = 0
value2 = 0
index = 0;
for i in a:
if i > 0.1:
value1 += 0.04
if (value2>0):
c.append(value2)
value2=0
else:
value2 += 0.04
if (value1 > 0):
b.append(value1)
value1=0
if value1 > 0 and index==(len(a) -1):
b.append(value1)
if value2 > 0 and index == (len(a) - 1):
c.append(value2)

index+=1

print(b)
print(c)


Related Topics



Leave a reply



Submit