1

What I want is to find the length of every column in a 2D NumPy array.

If all the columns have the same length, this is trivial with numpy.shape. Nevertheless, if the columns have different lengths, numpy.shape doesn't actually tell me the lengths of the different columns.

a=np.asarray([[0,1],[0,1],[0,1]])
b=np.asarray([[0,1],[0,1,2],[0]])
a.shape,b.shape
((3,2), (3,))

I can get what I want fairly simply by doing something like,

lenb=[len(B) for B in b]
[2, 3, 1]

However, I feel like there must be a cleaner and quicker way to do it with NumPy?

2
  • 3
    Cleaner : map(len,b)? Commented Apr 5, 2017 at 18:16
  • 4
    First, those are rows. Second, you should pretty much never try to create a jagged NumPy array like that. NumPy isn't designed for it, indexing will break, broadcasting will break, everything and the kitchen sink will break... if you really need to use a jagged data structure, don't make it an array. Commented Apr 5, 2017 at 18:37

1 Answer 1

4

Your b is an object array - 1d with list elements. Most actions on that array will require a list comprehension or map.

array([[0, 1], [0, 1, 2], [0]], dtype=object)

That 'object' dtype divides the array operations from the list ones. shape is an array property. len() is the closest list function, but it has to be applied to each element separately.

In Py3, I much prefer the clarity of a list comprehension to map, but that's just a preference. Functionally it's the same thing:

In [30]: [len(i) for i in b]
Out[30]: [2, 3, 1]
In [31]: list(map(len,b))
Out[31]: [2, 3, 1]

There is another possibility:

In [32]: np.frompyfunc(len,1,1)(b)
Out[32]: array([2, 3, 1], dtype=object)

You could change the elements of b to other objects with a len

In [39]: b[0]='abcd'    # string
In [43]: b[2]={1,2,1,3,4}   # set
In [44]: b
Out[44]: array(['abcd', [0, 1, 2], {1, 2, 3, 4}], dtype=object)
In [45]: [len(i) for i in b]
Out[45]: [4, 3, 4]

This should highlight the fact that len is a property of the elements, not the array or its 'columns' (which it doesn't have).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.