I'm working on some code which looks like the following:

import numpy as np

A=np.array([[1,0,3,5,7],[4,0,6,2,3]])

def SMD(matrix):
if isinstance(matrix,np.ndarray)==False:
    raise ValueError('The needed datatype is an array')
else:
    m= matrix.shape[0]
    n= matrix.shape[1]
    a=np.array([])
    b=np.array([0])
    c=np.array([])
    for i in range(m):
        for j in range(n):
            if matrix[i][j] !=0:            
                np.append(a,matrix[i][j])
                np.append(c,j)
        np.append(b,len(a))
    return a,b,c

However, the numpy append does not work for me in this case. If I instead use lists instead of arrays, the code runs just fine:

def SMD(matrix):
if isinstance(matrix,np.ndarray)==False:
    raise ValueError('The needed datatype is an array')
else:
    m= matrix.shape[0]
    n= matrix.shape[1]
    d=[]
    e=[0]
    f=[]
    for i in range(m):
        for j in range(n):
            if matrix[i][j] !=0:  
                d.append(matrix[i][j])
                f.append(j)
        e.append(len(d))
    return d,e,f

the wanted output is:

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

Or as arrays (depending on the code used).

Of course, I would like to know why the first code is not working.

From my knowledge, it can be preferable in terms of computation speed to use arrays, but in this case, does it make a difference?

Thanks

推荐答案

In terms of efficiency you you should avoid loops

def SMD(matrix):
    bool_matrix = (matrix!=0)
    return (
        matrix[bool_matrix],
        np.append(0, bool_matrix.sum(1).cumsum()),
        np.where(bool_matrix)[1]
    )

SMD(A)
#(array([1, 3, 5, 7, 4, 6, 2, 3]),
# array([0, 4, 8]),
# array([0, 2, 3, 4, 0, 2, 3, 4]))

matrix[bool_matrix] are simply all the non-zero elements of matrix

np.append(0, bool_matrix.sum(1).cumsum()) first compute the number of non-zero elements in the rows of matrix; then it compute the cumulative sum (from the first row to the last one); finally it add the 0 at the beginning of the array.

np.where(bool_matrix)[1] tells you the indices of the columns in which the elements of matrix are non-zero.

Python相关问答推荐

将列表理解分配给枚举值

如何聚合一个数据框然后用 Pandas 转置它

如何在 MultiIndexed Pandas DataFrame 中聚合行子集并附加到其中?

python - 如何通过Python selenium仅获取除子元素之外的div标签中的文本?

不能从另一个目录执行 Python 导入

使用 NumPy 和 OpenCV 有效地旋转图像并粘贴到更大的图像中

如何在每个其他元素处组合两个字符串列表?

Apache Airflow UI 显示 DAG 导入错误(IndexError: list index out of range)但 DAG 工作正常

如何舍入Pandas 数据框中一列集合中的值?

Panda 使用字典将数据帧相乘以映射列

函数将 DF 的所有列数据更改为相同的值,并忽略不同行的条件不同.并且 .Apply() 给出错误

如何将 rdkit DrawMorganBit 输出保存为图像?

导入使用 selenium 和 ipynb 的函数在 Windows 上不起作用

我可以在python中重载->吗?

从具有重复嵌套模式的文本文件中提取文本

更改字典中的键

修改多进程函数以接受参数列表

如何使用正则表达式替换单词之间的空格?

将一个列表中的当前元素与另一个列表中的下一个元素进行比较

具有最大组大小的 GroupBy