piRSquared piRSquared - 29 days ago 9
Python Question

I have an array of unequal length lists, what is quickest way to calculate the length of each

consider the array

a


a = np.array([
[list(range(np.random.randint(4, 10))) for _ in range(10)],
[list(range(np.random.randint(4, 10))) for _ in range(10)]
]).T

print(a)

[[[0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5]]
[[0, 1, 2, 3, 4, 5, 6, 7] [0, 1, 2, 3, 4, 5, 6]]
[[0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4, 5, 6, 7, 8]]
[[0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4]]
[[0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4, 5, 6, 7]]
[[0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4, 5, 6]]
[[0, 1, 2, 3, 4] [0, 1, 2, 3, 4]]
[[0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4, 5, 6, 7, 8]]
[[0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4, 5]]
[[0, 1, 2, 3, 4, 5, 6, 7] [0, 1, 2, 3, 4, 5, 6]





I'd expect output like this

[[7 6]
[8 7]
[9 9]
[9 5]
[6 8]
[6 7]
[5 5]
[9 9]
[9 6]
[8 7]]

Answer

To have it most efficient, I would suggest using a generator:

mygen = (map(len, row) for row in a)

This way you dont need to compute everything at once. You do it only when you need it. But I dont have any CPU benchmarks to back it up.