6
votes

I have a coordinated storage list in python A[row,col,value] for storing non-zeros values.

How can I get the list of all the row indexes? I expected this A[0:][0] to work as print A[0:] prints the whole list but print A[0:][0] only prints A[0].

The reason I ask is for efficient calculation of the number of non-zero values in each row i.e iterating over range(0,n) where n is the total number of rows. This should be much cheaper than my current way of for i in range(0,n): for j in A: ....

Something like:

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 (for returning the number of singleton rows) which is much faster for my current problems size of A than my original attempt. However it still grows with the number of rows and columns. I wonder if it's possible to not have to iterate over A but just upto n?

# get total list of row indexes from coordinate storage list
row_indexes = [i[0] for i in A]
# create dictionary {index:count}
c = Counter(row_indexes)    
# return only value where count == 1 
return [c[0] for c in c.items() if c[1] == 1]
2
@larsman: I assume A is a list of triples. - Junuxx
Can you write a simple, inefficient, working example of what you are trying to do? I find the wording of the question really confusing, and none of your example code-blocks seem to do the same thing..? - dbr
I am calculating all rows that contain only 1 non-zero value from a coordinate storage list. Only the 2nd code block slightly differs as it returned the count for every row. I have update the code with comments. - Chris Seymour

2 Answers

13
votes

This should do it:

c = [x[0] for x in A]

It's a list comprehension that takes the first (sub-)element of every element of A.

4
votes

For efficieny and extended slices, you can use numpy - which given your example seems like a good idea:

import numpy as np
yourlist = [
    [0, 0, 0],
    [0, 1, 1],
    [1, 0, 2]
]
a = np.array(yourlist)
print a[:,0]
# [0 0 1]
bc = np.bincount(a[:,0])
# array([2, 1])
count = bc[bc==1].size
# 1
# or... (I think it's probably better...)
count = np.count_nonzero(bc == 1)