简体   繁体   中英

In Python, how do I iterate over the rows of a sparse matrix?

This is what I am looking to do. x below is a sparse matrix.

for d in x:
    yvalue = foo(d[bf])

I consume yvalue afterward, just not pasting the code here. Assume that bf is computed somehow.

The above was my code for a dense matrix, but I need to find the equivalent code for a sparse matrix.

Would appreciate any help. Thanks

Maybe you do not want to use a matrix as the data structure of a sparse matrix. You may prefer list of list . This is more about algorithm and data structure than python.

Just like we use incidence matrix (2 dimensional array) to represent dense graph, but adjacency list (list of list) to represent sparse graph.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM