Home > Mobile >  Getting the minimum of each list in the array
Getting the minimum of each list in the array

Time:10-12

How can I write a numpy function that outputs the minimum of value of the multiple arrays within a and b. What could I add to the list comprehension below to be able to get the expected output.

import numpy as np

a= np.array([[12,2,1,3,45],[23,1,2],[5]], dtype=object)
b= np.array([[12,1,1,2334],[11,121,12]], dtype=object)

MaxDraw = np.array([min(draw) for draw in [a, b]])

Expected Output:

[[1], [1], [5]]
[[1], [11]]

CodePudding user response:

If you use np.array for a list which its elements size are not equal, it will be like:

>>> np.array([[12,2,1,3,45],[23,1,2],[5]])
array([list([12, 2, 1, 3, 45]), list([23, 1, 2]), list([5])], dtype=object)

So basically, numpy function will not work with your array. For your problem, I think a simple solution is enough.

>>> a = [[12,2,1,3,45],[23,1,2],[5]]
>>> [min(mini_list) for mini_list in a]
[1, 1, 5]

CodePudding user response:

As you are using an object dtype (to hold lists of differents sizes), using numpy doesn't really make sense as you will lose the benefit of numpy vector functions.

Anyway, you need:

np.array([[[min(draw)] for draw in l] for l in [a, b]])

output:

array([list([[1], [1], [5]]), list([[1], [11]])], dtype=object)
  • Related