我正在将输入参数记录到函数中,使用

logging.debug('Input to this function = %s',
              inspect.getargvalues(inspect.currentframe())[3])

但我不希望在numpy对象中插入换行符。numpy.set_printoptions(linewidth=np.nan)删除一些,但换行符仍插入二维对象中,例如
array([[ 0.84148239,  0.71467895,  0.00946744,  0.3471317 ],
       [ 0.68041249,  0.20310698,  0.89486761,  0.97799646],
       [ 0.22328803,  0.32401271,  0.96479887,  0.43404245]])

我希望它是这样的:
array([[ 0.84148239,  0.71467895,  0.00946744,  0.3471317 ], [ 0.68041249,  0.20310698,  0.89486761,  0.97799646], [ 0.22328803,  0.32401271,  0.96479887,  0.43404245]])

我该怎么做?谢谢。

最佳答案

给定一个数组,可以打印它而不使用换行符,

import numpy as np
x_str = np.array_repr(x).replace('\n', '')
print(x_str)

或者使用函数x而不是np.array2string
我不确定是否有一种简单的方法可以从字符串表示或numpy数组中删除换行符。但是,在转换发生后总是可以移除它们,
input_args = inspect.getargvalues(inspect.currentframe())[3]
logging.debug('Input to this function = %s', repr(input_args).replace('\n', ''))

关于python - 如何打印没有换行符的numpy对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29102955/

10-10 09:52
查看更多