TypeError: 'numpy.int64' object is not iterable ,'int' object is not iterable
想用一個list來動態(tài)地增加numpy類型數(shù)據(jù),如下面的代碼所示,發(fā)現(xiàn)報錯TypeError: 'numpy.int64' object is not iterable?
a = [] b = np.array([1,2,3]) a.extend(b[0]) a.extend(b[1]) a.extend(b[2]) print(a)于是將numpy數(shù)據(jù)轉(zhuǎn)為list類型,如下所示:
a = [] b = np.array([1,2,3]) a.extend(b[0].tolist()) a.extend(b[1].tolist()) a.extend(b[2].tolist()) print(a)發(fā)現(xiàn)報錯:TypeError: 'int' object is not iterable
通過打印‘b[0].tolist()’的類型,發(fā)現(xiàn)‘b[0].tolist()’的類型是‘int’,即還是沒有把‘b[0].tolist()’轉(zhuǎn)為list類型
再修改代碼如下,通過加個中括號[]把‘b[0].tolist()’轉(zhuǎn)為list類型
a = [] b = np.array([1,2,3]) a.extend([b[0].tolist()]) a.extend([b[1].tolist()]) a.extend([b[2].tolist()]) print(a) #[1, 2, 3]-------------------------------------------------------------------------分割線--------------------------------------------------------------------------------------------------
后來我發(fā)現(xiàn)直接用下面的代碼也可以解決:
a = [] b = np.array([1,2,3]) a.extend([b[0]]) a.extend([b[1]]) a.extend([b[2]]) print(a) #[1, 2, 3]這是因為通過加個中括號[]把‘b[0]’從numpy數(shù)據(jù)類型轉(zhuǎn)為了list類型
總結(jié)
以上是生活随笔為你收集整理的TypeError: 'numpy.int64' object is not iterable ,'int' object is not iterable的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pytorch两种常用的学习率衰减方法
- 下一篇: np.squeeze()