我想將A
包含字串元素的陣列轉換為串列。但是我在使用時遇到了錯誤split()
。我提出了預期的輸出。
import numpy as np
from numpy import nan
A=np.array(['[1]', '[2]', '[3]', '[4]', '[5]'])
A.split()
print(A)
錯誤是
in <module>
A.split()
AttributeError: 'list' object has no attribute 'split'
預期的輸出是
array([[1], [2], [3], [4], [5]])
uj5u.com熱心網友回復:
我認為這應該有效
A=np.array(['[1]', '[2]', '[3]', '[4]', '[5]'])
output = [[int(a.strip("[|]"))] for a in A]
print(output)
[[1], [2], [3], [4], [5]]
如果您只喜歡一個串列,而不是串列串列
A=np.array(['[1]', '[2]', '[3]', '[4]', '[5]'])
output = [int(a.strip("[|]")) for a in A]
print(output)
[1, 2, 3, 4, 5]
uj5u.com熱心網友回復:
import numpy as np
A = np.array(['[1]', '[2]', '[3]', '[4]', '[5]'])
B = np.array([int(item.strip("'\'").replace("[","").replace("]","")) for item in A] )
print(B) #[1 2 3 4 5]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507397.html