TypeError: 'numpy.int64' object is not iterable ,'int' object is not iterable
生活随笔
收集整理的這篇文章主要介紹了
TypeError: 'numpy.int64' object is not iterable ,'int' object is not iterable
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
想用一個list來動態地增加numpy類型數據,如下面的代碼所示,發現報錯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數據轉為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)發現報錯:TypeError: 'int' object is not iterable
通過打印‘b[0].tolist()’的類型,發現‘b[0].tolist()’的類型是‘int’,即還是沒有把‘b[0].tolist()’轉為list類型
再修改代碼如下,通過加個中括號[]把‘b[0].tolist()’轉為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]-------------------------------------------------------------------------分割線--------------------------------------------------------------------------------------------------
后來我發現直接用下面的代碼也可以解決:
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數據類型轉為了list類型
總結
以上是生活随笔為你收集整理的TypeError: 'numpy.int64' object is not iterable ,'int' object is not iterable的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pytorch两种常用的学习率衰减方法
- 下一篇: JavaSE——IO(上)(File、字