[英]How to print numpy objects without line breaks
我將輸入 arguments 記錄到 function 使用
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
np.set_printoptions(threshold=np.inf)
np.set_printoptions(linewidth=np.inf)
# Testing:
big_arr = np.ones([30,70])
print(big_arr)
簡單的解決方案
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(' ',', '))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.