-1

I have a tuple of arrays and need to find the length of an array by using the index of the tuple. This tuple is generated from a sparse matrix alternative to numpy where i'm using scipy.sparse.csr_matrix and numpy.unravel_index based on this post

ie:

>>>T = [(array([0,1,2,3,4,5]),), (array([0,1,2,3,4,5,6,7,8,9]),)]
>>>print(T[1])
(array([0,1,2,3,4,5,6,7,8,9]),)

I need to find the length of each array within the tuple for use later. using len() does not work

>>>len(T[1])
1

I am using this within a for loop that iterates through each array using those arrays as an index for other data.

I have searched and searched for how to solve this but found nothing. Please help!

Sourav
  • 393
  • 2
  • 18

2 Answers2

0

Each item in T is a tuple of length 1, so just index it, like this:

>>> len(T[1][0])
10

To be clear:

>>> T[1]
(array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),)
>>> T[1][0]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

You can try comprehension concept as well,like

arr_length = [len(i[0]) for i in T]