I have a coordinated storage list in python A[row,col,value] to store values โโwithout zero.
How can I get a list of all row indices? I expected this A[0:][0] work as print A[0:] prints the entire list, but print A[0:][0] prints only A[0] .
The reason I'm asking is to efficiently calculate the number of non-zero values โโin each row, iterate over range(0,n) , where n is the total number of rows. This should be much cheaper than my current way for i in range(0,n): for j in A: ...
Sort of:
c = [] # for the total number of rows for i in range(0,n): # get number of rows with only one entry in coordinate storage list if A[0:][0].count(i) == 1: c.append(i) return c
Over:
c = [] # for the total number of rows for i in range(0,n): # get the index and initialize the count to 0 c.append([i,0]) # for every entry in coordinate storage list for j in A: # if row index (A[:][0]) is equal to current row i, increment count if j[0] == i: c[i][1]+=1 return c
EDIT:
Using Junuxx's answer, this question and this post I came up with the following (to return the number of single-line lines), which is much faster for my current problems of size A than my initial attempt. However, it is still growing with the number of rows and columns. I wonder if itโs possible not to iterate over A , but just up to n ?
# get total list of row indexes from coordinate storage list row_indexes = [i[0] for i in A]
source share