您无需声明 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 维数组,然后将其转换为 string,因此当您调用 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 数组呈现为 string,那么您可以执行以下操作:
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