Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何向numpy矩阵中的某个元素添加非数字字符?_Python_List_Numpy_Matrix_Character - Fatal编程技术网

Python 如何向numpy矩阵中的某个元素添加非数字字符?

Python 如何向numpy矩阵中的某个元素添加非数字字符?,python,list,numpy,matrix,character,Python,List,Numpy,Matrix,Character,假设我有一个numpy矩阵: matrix = np.zeros((5,5)) # goes through code that changes value of matrix to get: [[0. 4. 7. 1. 3.] [5. 6. 8. 0. 1.] [inf inf 5. 2. 5.] [4. 6. 2. 1. inf]] 打印此矩阵时,我想在某些元素的任一侧添加两个星号: [[0. *4* 7. 1. 3.] [5. 6. *8* 0. 1.] [inf inf

假设我有一个numpy矩阵:

matrix = np.zeros((5,5))

# goes through code that changes value of matrix to get:

[[0. 4. 7. 1. 3.]
 [5. 6. 8. 0. 1.]
 [inf inf 5. 2. 5.]
 [4. 6. 2. 1. inf]]
打印此矩阵时,我想在某些元素的任一侧添加两个星号:

[[0. *4* 7. 1. 3.]
 [5. 6. *8* 0. 1.]
 [inf inf 5. 2. 5.]
 [4. *6* 2. *1* inf]]
我该怎么做呢

我试过:

 matrix_output = matrix
 matrix_output[i][j] = '*' + str(matrix_for_print[i][j]) + '*'
但我得到了这个错误:

 builtins.ValueError: could not convert string to float: '*4.0*'
(另外,请注意,如何使矩阵只包含逗号而不是此处的小数。因此,理想情况下,我希望:

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

我想知道这些调整是否真的值得?但首先要确保您了解在每个阶段都有哪些对象,以及为什么它们会这样显示

从类似您的简单阵列开始:

In [163]: arr = np.array([[0, 4,7],[np.inf, 0,3]])                                                           
In [164]: arr                                                                                                
Out[164]: 
array([[ 0.,  4.,  7.],
       [inf,  0.,  3.]])
In [165]: arr.dtype                                                                                          
Out[165]: dtype('float64')
即使数字是整数,数据类型也是浮点。
np.inf
是浮点。
np.zeros
默认为浮点

您可以将其更改为列表列表:

In [166]: alist = arr.tolist()                                                                               
In [167]: alist                                                                                              
Out[167]: [[0.0, 4.0, 7.0], [inf, 0.0, 3.0]]
请注意,此显示包括逗号和0,并且不会将其划分为行。但作为一个列表,我们可以将单个项目更改为字符串:

In [168]: alist[0][2] = '*7*'                                                                                
In [169]: alist                                                                                              
Out[169]: [[0.0, 4.0, '*7*'], [inf, 0.0, 3.0]]
甚至回到字符数组:

In [170]: np.array(alist)                                                                                    
Out[170]: 
array([['0.0', '4.0', '*7*'],
       ['inf', '0.0', '3.0']], dtype='<U32')

并更改字符串。请注意数据类型大小。这是32个字符宽的。如果是
,则听起来像是在混淆数据和演示文稿。如果希望在向用户演示输出时在输出中使用星号,则应在输出格式代码中使用星号,而不是试图在arr中粘贴一堆星号ay本身。数组表示数字数据。@user2357112是的,这就是我要做的,意思是用“*”打印矩阵。我只是不知道如何打印它本身。
matrix
现在是
ndarray
with
float
dtype。
inf
是float。
alist=matrix.tolist()
,创建列表的嵌套列表。然后,您可以将元素从数字更改为字符串
“*8*”
。列表显示还包括逗号(numpy数组通常省略这些字符)。
In [171]: arr.astype(str)                                                                                    
Out[171]: 
array([['0.0', '4.0', '7.0'],
       ['inf', '0.0', '3.0']], dtype='<U32')
In [172]: _[0,2] = '*7*'                                                                                     
In [173]: _171                                                                                               
Out[173]: 
array([['0.0', '4.0', '*7*'],
       ['inf', '0.0', '3.0']], dtype='<U32')
In [174]: arr.astype(int)                                                                                    
Out[174]: 
array([[                   0,                    4,                    7],
       [-9223372036854775808,                    0,                    3]])
In [185]: astr = []                                                                                          
In [186]: for row in arr: 
     ...:     str1 = ['%s'%i for i in row] 
     ...:     astr.append(str1) 
     ...:                                                                                                    
In [187]: '\n'.join([', '.join(row) for row in astr])                                                        
Out[187]: '0.0, 4.0, 7.0\ninf, 0.0, 3.0'
In [188]: print(_)                                                                                           
0.0, 4.0, 7.0
inf, 0.0, 3.0