Stick with Numpy array and use its sum() method:
>>> arr = np.array([[1,2,3,5,4,3],
[5,7,2,4,6,7],
[3,6,2,4,5,9]])
>>> arr.sum(axis=0)
array([ 9, 15, 7, 13, 15, 19])
Of course you can do it with Python lists as well but it is going to be slow:
>>> lst = [[1,2,3,5,4,3],
[5,7,2,4,6,7],
[3,6,2,4,5,9]]
>>> map(sum, zip(*lst))
[9, 15, 7, 13, 15, 19]
Answer from Ashwini Chaudhary on Stack OverflowSum of Two Arrays - Python - Stack Overflow
python - Summing each element of two arrays - Stack Overflow
python - Add SUM of values of two LISTS into new LIST - Stack Overflow
How to sum two arrays in Python? - Stack Overflow
Stick with Numpy array and use its sum() method:
>>> arr = np.array([[1,2,3,5,4,3],
[5,7,2,4,6,7],
[3,6,2,4,5,9]])
>>> arr.sum(axis=0)
array([ 9, 15, 7, 13, 15, 19])
Of course you can do it with Python lists as well but it is going to be slow:
>>> lst = [[1,2,3,5,4,3],
[5,7,2,4,6,7],
[3,6,2,4,5,9]]
>>> map(sum, zip(*lst))
[9, 15, 7, 13, 15, 19]
There is no need to create a 2D array from your pre-existing 1D arrays. It will certainly not be faster than adding them together, e.g. using reduce with np.add:
In [14]: a = [np.random.rand(10) for _ in range(10)]
In [15]: %timeit np.array(a).sum(axis=0)
100000 loops, best of 3: 10.7 us per loop
In [16]: %timeit reduce(np.add, a)
100000 loops, best of 3: 5.24 us per loop
For larger arrays, it is even less advantageous:
In [17]: a = [np.random.rand(1000) for _ in range(1000)]
In [18]: %timeit np.array(a).sum(axis=0)
100 loops, best of 3: 6.26 ms per loop
In [19]: %timeit reduce(np.add, a)
100 loops, best of 3: 2.43 ms per loop
And of course:
In [20]: np.allclose(np.array(a).sum(axis=0), reduce(np.add, a))
Out[20]: True
You can use numpy.
import numpy as np
a = np.array(arrayOne)
b = np.array(arrayTwo)
max = max(a + b)
print(max)
Use itertools.product with max:
from itertools import product
print(max(sum(x) for x in product(arrayOne, arrayTwo)))
Or using map:
print(max(map(sum,product(arrayOne, arrayTwo))))
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]
Default behavior in numpy.add (numpy.subtract, etc) is element-wise:
import numpy as np
np.add(first, second)
which outputs
array([7,9,11,13,15])
print(df.loc[df['year'].isin((1851,1852))]["occur"].sum())
Or:
print(df.loc[df.year.isin((1851,1852))].occur.sum())
For a range of dates creating a list of ranges seems more efficient than using &:
df.loc[df.year.isin(range(s1, s2+1))].occur.sum()
If you're specifically wanting only a numpy approach, you'd do something similar to this:
import numpy as np
occurrence= np.array([4, 5, 4, 0, 1, 4, 3])
year = np.array([1851,1852,1853,1854,1855,1856,1857])
year1, year2 = 1851, 1852
mask = (year == year1) | (year == year2)
print occurrence[mask].sum()
Note that if you wanted the sum of all occurences between those two years, you'd do something more like:
mask = (year >= year1) & (year <= year2)
With pandas, the same approach still works, but as others have noted, there are more efficient ways of building the boolean mask with the isin method, if you're interested in just those two years (and not the interval between them).
Well, all other answers are awesome for adding 2 numbers (list of digits).
But in case you want to create a program which can deal with any number of 'numbers',
Here's what you can do...
def addNums(lst1, lst2, *args):
numsIters = [iter(num[::-1]) for num in [lst1, lst2] + list(args)] # make the iterators for each list
carry, final = 0, [] # Initially carry is 0, 'final' will store the result
while True:
nums = [next(num, None) for num in numsIters] # for every num in numIters, get the next element if exists, else None
if all(nxt is None for nxt in nums): break # If all numIters returned None, it means all numbers have exhausted, hence break from the loop
nums = [(0 if num is None else num) for num in nums] # Convert all 'None' to '0'
digit = sum(nums) + carry # Sum up all digits and carry
final.append(digit % 10) # Insert the 'ones' digit of result into final list
carry = digit // 10 # get the 'tens' digit and update it to carry
if carry: final.append(carry) # If carry is non-zero, insert it
return final[::-1] # return the fully generated final list
print(addNums([6, 9, 8], [5, 9, 2])) # [1, 2, 9, 0]
print(addNums([7, 6, 9, 8, 8], [5, 9, 2], [3, 5, 1, 7, 4])) # [1, 1, 2, 7, 5, 4]
Hope that makes sense!
If I understand correctly you want it like this: [6, 9, 8], [5, 9, 2] -> 698 + 592 = 1290 -> [1, 2, 9, 0]
In that case my first idea would be to turn the numbers into strings, combine them to one string and turn it into an int, then add both values together and turn into a list of integers again... you can try this:
def get_sum_as_list(list1, list2):
first_int = int(''.join(map(str,list1)))
second_int = int(''.join(map(str,list2)))
result = [int(num) for num in str(first_int+second_int)]
return result
I'm given an array of ('string',2.0),('string',3.0) and I don't how how to sum up the floats only?