如何在没有换行符的情况下打印numpy对象
How to print numpy objects without line breaks
我正在使用
将输入参数记录到一个函数
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]])
我该怎么做?谢谢
给定一个数组 x
,你可以不换行地打印它,
import numpy as np
x_str = np.array_repr(x).replace('\n', '')
print(x_str)
或者使用函数 np.array2string
而不是 np.array_repr
.
我不确定是否有一种简单的方法可以从字符串表示形式或 numpy 数组中删除换行符。但是,总是可以在转换发生后删除它们,
input_args = inspect.getargvalues(inspect.currentframe())[3]
logging.debug('Input to this function = %s', repr(input_args).replace('\n', ''))
简单的解决方案
import numpy as np
value = np.array([[1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 7, 8]])
value_str = str(value).replace('\n', '')
print("prints: " + value_str)
# prints: [[1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 7, 8]]
此解决方案也有效:
myArray = np.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]])
print(np.array2string(myArray).replace('\n','').replace(' ',', '))
输出:
[[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
np.set_printoptions(threshold=np.inf)
np.set_printoptions(linewidth=np.inf)
# Testing:
big_arr = np.ones([30,70])
print(big_arr)
我正在使用
将输入参数记录到一个函数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]])
我该怎么做?谢谢
给定一个数组 x
,你可以不换行地打印它,
import numpy as np
x_str = np.array_repr(x).replace('\n', '')
print(x_str)
或者使用函数 np.array2string
而不是 np.array_repr
.
我不确定是否有一种简单的方法可以从字符串表示形式或 numpy 数组中删除换行符。但是,总是可以在转换发生后删除它们,
input_args = inspect.getargvalues(inspect.currentframe())[3]
logging.debug('Input to this function = %s', repr(input_args).replace('\n', ''))
简单的解决方案
import numpy as np
value = np.array([[1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 7, 8]])
value_str = str(value).replace('\n', '')
print("prints: " + value_str)
# prints: [[1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, 7, 8]]
此解决方案也有效:
myArray = np.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]])
print(np.array2string(myArray).replace('\n','').replace(' ',', '))
输出:
[[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
np.set_printoptions(threshold=np.inf)
np.set_printoptions(linewidth=np.inf)
# Testing:
big_arr = np.ones([30,70])
print(big_arr)