Skip to main content
 首页 » 编程设计

python之python3.4 的 numpy 矩阵字符串

2025年05月04日23tintown

我在使用 numpy 时遇到了 3.4 的问题。我的问题是要知道我怎样才能拥有一个带有纯字符串格式而不是字节字符串的 numpy 矩阵。

def res(data): 
    M = np.zeros(data.shape).astype(dtype='|S20') 
    lines,columns = M.shape 
    for l in range(lines): 
        M[l][0] = data[l][1] 
        M[l][1] = data[l][2] 
        M[l][2] = data[l][3]   
    return M 
**result python2.7** 
[['Ann' '38.72' '-9.133'] 
 ['John' '55.68' '12.566'] 
 ['Richard' '52.52' '13.411'] 
 ['Alex' '40.42' '-3.703']] 
 
**result python3.4** 
[[b'Ann' b'38.72' b'-9.133'] 
 [b'John' b'55.68' b'12.566'] 
 [b'Richard' b'52.52' b'13.411'] 
 [b'Alex' b'40.42' b'-3.703']] 

在 Python3.4 中,我如何才能像 python2.7 中的示例那样在纯字符串中使用我的矩阵,这很糟糕,因为我有需要字符串值而不是字节字符串的函数。

任何帮助都会很棒。谢谢

请您参考如下方法:

在我的例子中,解决方案只是将 dtype('|S20') 更改为 dtype(str)..我希望这会有所帮助。