Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

5 of 5
Commonmark migration

You can use zip() to zip together each list and each sublist to compare them element-wise:

Make an iterator that aggregates elements from each of the iterables.

Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. [...].

>>> def max_value(lst1, lst2):
 for subl1, subl2 in zip(lst1, lst2):
 for el1, el2 in zip(subl1, subl2):
 yield max(el1, el2)
 
>>> 
>>> a=[[2,4],[6,8]]
>>> b=[[1,7],[5,9]]
>>> 
>>> list(max_value(a, b))
[2, 7, 6, 9]

If using NumPy, you can use numpy.maximum():

Element-wise maximum of array elements.

Compare two arrays and returns a new array containing the element-wise maxima. [...].

>>> import numpy as np
>>> 
>>> a = np.array([[2,4],[6,8]])
>>> b = np.array([[1,7],[5,9]])
>>> 
>>> np.maximum(a, b)
array([[2, 7],
 [6, 9]])
>>> 
Chris
  • 23.2k
  • 8
  • 63
  • 91

AltStyle によって変換されたページ (->オリジナル) /