您不需要声明array()
来完成您尝试做的事情:
A = [0, 0, 0, 0, 0.64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
print(A)
[0, 0, 0, 0, 0.64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
print(f'{A}')
[0, 0, 0, 0, 0.64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
A
[0, 0, 0, 0, 0.64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
看起来你正在创建一个NumPy N维数组,然后将其转换为字符串,因此在调用print()
时打印了该数组的字符串表示形式。除非你特别需要NumPy数组,否则你可以像上面那样做,或者如果你需要:
from numpy import array
A = [0, 0, 0, 0, 0.64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
B = array(A)
print(B)
[0. 0. 0. 0. 0.64 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 1. 0. 0. 0. ]
print(f'{B}')
[0. 0. 0. 0. 0.64 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 1. 0. 0. 0. ]
B
array([0. , 0. , 0. , 0. , 0.64, 0. , 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. ])
如果您必须将NumPy数组呈现为
字符串
,则可以像这样操作:
text = f'{A}'
text = text.replace("\n","")
正如Ramón Márquez所提到的那样,您可以简单地增加printoptions
的行宽:
numpy.set_printoptions(linewidth=96)
NumPy数组的文档:https://machinelearningmastery.com/gentle-introduction-n-dimensional-arrays-python-numpy/
NumPy打印选项的文档:https://numpy.org/doc/1.18/reference/generated/numpy.printoptions.html
text = ' '.join(i.strip() for i in str(A))
。 - Mohnish