且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

替换 numpy ndarray 中的字符(Python)

更新时间:2023-11-26 10:06:16

你可以使用 NumPy 的 core.defchararray 处理与字符串相关的操作,在这种情况下使用 replace 方法,像这样 -

You could use NumPy's core.defchararray that deals with string related operations and for this case use replace method, like so -

np.core.defchararray.replace(arr,'\t', '    ')

样品运行 -

In [44]: arr
Out[44]: 
array(['\tblah blah', '"""123', 'blah', '"""', '\t456', '78\t9'], 
      dtype='|S10')

In [45]: np.core.defchararray.replace(arr,'\t', '    ')
Out[45]: 
array(['    blah blah', '"""123', 'blah', '"""', '    456', '78    9'], 
      dtype='|S13')