Asked  7 Months ago    Answers:  5   Viewed   43 times

I have now:

list1 = [1, 2, 3]
list2 = [4, 5, 6]

I wish to have:

[1, 2, 3]
 +  +  +
[4, 5, 6]
|| || ||
[5, 7, 9]

Simply an element-wise addition of two lists.

I can surely iterate the two lists, but I don't want do that.

What is the most Pythonic way of doing so?

 Answers

41

Use map with operator.add:

>>> from operator import add
>>> list( map(add, list1, list2) )
[5, 7, 9]

or zip with a list comprehension:

>>> [sum(x) for x in zip(list1, list2)]
[5, 7, 9]

Timing comparisons:

>>> list2 = [4, 5, 6]*10**5
>>> list1 = [1, 2, 3]*10**5
>>> %timeit from operator import add;map(add, list1, list2)
10 loops, best of 3: 44.6 ms per loop
>>> %timeit from itertools import izip; [a + b for a, b in izip(list1, list2)]
10 loops, best of 3: 71 ms per loop
>>> %timeit [a + b for a, b in zip(list1, list2)]
10 loops, best of 3: 112 ms per loop
>>> %timeit from itertools import izip;[sum(x) for x in izip(list1, list2)]
1 loops, best of 3: 139 ms per loop
>>> %timeit [sum(x) for x in zip(list1, list2)]
1 loops, best of 3: 177 ms per loop
Tuesday, June 1, 2021
 
HexaGridBrain
answered 7 Months ago
81

Use a list comprehension mixed with zip():.

[a*b for a,b in zip(lista,listb)]
Sunday, June 6, 2021
 
superfell
answered 6 Months ago
56

I assume the error occurs in this expression:

np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1))

can you tell us something about the 2 arrays, predictions, labels? The usual stuff - dtype, shape, some sample values. Maybe go the extra step and show the np.argmax(...) for each.

In numpy you can compare arrays of the same size, but it has become pickier about comparing arrays that don't match in size:

In [522]: np.arange(10)==np.arange(5,15)
Out[522]: array([False, False, False, False, False, False, False, False, False, False], dtype=bool)
In [523]: np.arange(10)==np.arange(5,14)
/usr/local/bin/ipython3:1: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.
  #!/usr/bin/python3
Out[523]: False
Sunday, August 15, 2021
 
dannyhobby
answered 4 Months ago
24

There's an alternative zip that does not stop at the shortest: itertools.zip_longest(). You can specify a fill value for the shorter lists:

from itertools import zip_longest

result = [sum(n) for n in zip_longest(a, b, fillvalue=0)]
Monday, August 30, 2021
 
rojo
answered 3 Months ago
15

expand.grid(list.a, list.b) gives you the desired result in a data.frame. This tends to be the most useful format for working with data in R. However, you could get the exact structure you ask for (save the ordering) with a call to apply and lapply:

result.df <- expand.grid(list.a, list.b)
result.list <- lapply(apply(result.df, 1, identity), unlist)

If you want this list ordered by the first element:

result.list <- result.list[order(sapply(result.list, head, 1))]
Friday, October 29, 2021
 
Aaron
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share