2014-01-24 41 views
1

存储字符串创建如下:在numpy的矩阵

a=np.eye(2, dtype='S17') 

但是,当我打印,我得到:

print(a) 
[[b'1' b''] 
[b'' b'1']] 

它为什么会发生,我能做些什么,以刚刚得到的字符串,但不b'或者我应该改变介绍数据的方式还是dtype

所需的输出将是:

[['1' ''] 
['' '1']] 

所以,我可以通过其他

+0

为什么不改变你dtype为int – agconti

+0

因为我想在那里存储字符串,所以当我创建矩阵时,我首先说出我将要放入的数据类型,然后添加它。 – Llopis

+1

http://stackoverflow.com/questions/6269765/what-does-the-b-character-do-in-front-of-a-string-literal –

回答

2

您可以使用numpy.char.decode到字节文字解码替换此字符串:

In [1]: import numpy as np 

In [2]: a = np.eye(2, dtype='S17')                     

In [3]: a 
Out[3]: 
array([[b'1', b''],                         
     [b'', b'1']],                        
     dtype='|S17')                         

In [4]: np.char.decode(a, 'ascii')                     
Out[4]: 
array([['1', ''],                         
     ['', '1']],                         
     dtype='<U1') 
+2

而不是'dtype ='S17''把它改成' ' Llopis