for循环读取数组遇问题:dexError: invalid index to scalar variable.

用for循环从文档中读取数组时,遇到一个bug,只能循环完成第一个循环,第二个循环就报错:dexError: invalid index to scalar variable.

其实是变量设置的问题,代码如下:

...
with open(save_path, 'rb') as fi1:
        result = pickle.load(fi1, encoding='iso-8859-1')
    name, fx = zip(*result)  
    
for i in range(0, 10):
    fx = np.array(fx)[i]  
    f_top_id = fx.argsort()[::-1][0:10]  # 排序,得到由高到低的序号
    print('i & fx:',i, fx[f_top_id[i]])
...

上面这段代码就是想从文档中的矩阵中一行接一行的提取数组,在for循环中读取第一行没问题,读出第二行就报错,其实原因很简单,低级错误,弄了几个小时debug,太不注意了,之前也遇到过。
问题出在fx这里,从name, fx = zip(*result)得到后,放入循环中又继续用这个变量名fx = np.array(fx)[i],这就炸锅了,第二次循环就肯定报错,因为fx在第一个循环的参数又直接导入到第二个循环中的fx,这就是问题所在,实际上需要fx的数据保持不变。
解决方法:在for循环中的fx改成其他变量名就解决了,如f_x就可以了,改完如下:

...
with open(save_path, 'rb') as fi1:
        result = pickle.load(fi1, encoding='iso-8859-1')
    name, fx = zip(*result)  
    
for i in range(0, 10):
    f_x = np.array(fx)[i]  
    f_top_id = f_x.argsort()[::-1][0:10]  # 排序,得到由高到低的序号
    print('i & fx:',i, f_x[f_top_id[i]])
...

记录debug的问题,加深记忆,也希望能给其他遇到同样报错的人一些启发,也感谢其他人的分享。

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
You are visitor as of October 17, 1996.The Art of Assembly Language ProgrammingForward Why Would Anyone Learn This Stuff?1 What's Wrong With Assembly Language2 What's Right With Assembly Language?3 Organization of This Text and Pedagogical Concerns4 Obtaining Program Source Listings and Other Materials in This TextSection One: Machine OrganizationArt of Assembly Language: Chapter OneChapter One - Data Representation1.0 - Chapter Overview1.1 - Numbering Systems1.1.1 - A Review of the Decimal System1.1.2 - The Binary Numbering System1.1.3 - Binary Formats1.2 - Data Organization1.2.1 - Bits1.2.2 - Nibbles1.2.3 - Bytes1.2.4 - Words1.2.5 - Double Words1.3 - The Hexadecimal Numbering System1.4 - Arithmetic Operations on Binary and Hexadecimal Numbers1.5 - Logical Operations on Bits1.6 - Logical Operations on Binary Numbers and Bit Strings1.7 - Signed and Unsigned Numbers1.8 - Sign and Zero Extension1.9 - Shifts and Rotates1.10 - Bit Fields and Packed Data1.11 - The ASCII Character Set1.12 Summary1.13 Laboratory Exercises1.13.1 Installing the Software1.13.2 Data Conversion Exercises1.13.3 Logical Operations Exercises1.13.4 Sign and Zero Extension Exercises1.13.5 Packed Data Exercises1.14 Questions1.15 Programming ProjectsChapter Two - Boolean Algebra2.0 - Chapter Overview2.1 - Boolean Algebra2.2 - Boolean Functions and Truth Tables2.3 - Algebraic Manipulation of Boolean Expressions2.4 - Canonical Forms2.5 - Simplification of Boolean Functions2.6 - What Does This Have To Do With Computers, Anyway?2.6.1 - Correspondence Between Electronic Circuits and Boolean Functions2.6.2 - Combinatorial Circuits2.6.3 - Sequential and Clocked Logic2.7 - Okay, What Does It Have To Do With Programming, Then?2.8 - Generic Boolean Functions2.9 Laboratory Exercises<
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值