矩陣的某幾行變換成字串在拼接到一起函式實現

weixin_42053726發表於2018-12-26

 

 


def string_joint(matrix,row):
    '''
    matrix:numpy矩陣格式
    row:列表,如[0,1,2,5] 意思是將矩陣的第0,1,2,5 行變成字串拼接到一起,最後返回列表

    '''
    temp =[]
    for i in range(matrix.shape[1]):
        str_temp = ''
        for j in row:
            str_temp = str(str_temp) +str(matrix[j][i])
        temp.append(str_temp)
    return temp

#-----------------測試
a=np.array([['a','a','123'],
           ['b','c','456'],
            [1,2,345]])

x = string_joint(a,[0,1,2])
print(x)

相關文章