我有一個要匯出為 txt 檔案的陣列串列。
x = [[np.array([0, 0, 0]), np.array([1,2,2])], [np.array([0, 0, 0]), np.array([4,5,6])]]
所需的輸出形狀:
(0,0,0),(1,2,2)
(0,0,0),(4,5,6)
我嘗試使用以下所有方法,但沒有得到我需要的相同東西。
# Trial1
np.savetxt(full_path, x, delimiter = ',')
錯誤:預期 1D 或 2D 陣列,改為 3D 陣列
# Trial2
np.save(full_path, x, allow_pickle=True)
# Not possible, can't handle the extension from pickle
..
# Trial3
df = pd.DataFrame(x)
print(df.to_csv(index=False, header=False))
輸出接近但不是我要找的
[0 0 0],[1 2 2]
[0 0 0],[4 5 6]
還有其他想法嗎?
uj5u.com熱心網友回復:
您可以將 numpy 陣列轉換為元組,因為它們的字串表示使用括號而不是方括號。
arrays = [[np.array([0, 0, 0]), np.array([1,2,2])], [np.array([0, 0, 0]), np.array([4,5,6])]]
linesToFile = [str(tuple(x[0])) ',' str(tuple(x[1])) for x in arrays]
with open(full_path, "w") as f:
for line in linesToFile:
f.write(line "\n")
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/505102.html
上一篇:Java呼叫C++元件——Jni
下一篇:在Python中附加兩個陣列
