TypeError
1、only size-1 arrays can be converted to Python scalars
问题来源:需要把一个float数组A转为int,于是直接在代码中写了 B=int(A),从而报错。
原因:int函数只能对单个数字进行,而不能对一个数组进行
解决方法:用map函数,对数组中的每个元素整数化
B=list( map( int , A ) )
在尝试将浮点数数组转换为整数时遇到了TypeError,原因是int()函数不接受数组作为参数。解决方法是使用map()函数,遍历数组并对每个元素应用int()函数。例如,将B=list(map(int,A))添加到代码中即可完成转换。
TypeError
1、only size-1 arrays can be converted to Python scalars
问题来源:需要把一个float数组A转为int,于是直接在代码中写了 B=int(A),从而报错。
原因:int函数只能对单个数字进行,而不能对一个数组进行
解决方法:用map函数,对数组中的每个元素整数化
B=list( map( int , A ) )
3868

被折叠的 条评论
为什么被折叠?