当前位置:   article > 正文

解决“ TypeError: ‘float‘ object cannot be interpreted as an integer”_typeerror: 'float' object cannot be interpreted as

typeerror: 'float' object cannot be interpreted as an integer

记录python运行中的小错误,希望对你有用。

报错问题描述:

  1. Traceback (most recent call last):
  2. File "D:/A-myself/code/new/Blind-Watermark-fft/Blind-Watermark-master/encode.py", line 22, in <module>
  3. random.shuffle(x)
  4. File "D:\Users\admin\anaconda3\envs\torch365\lib\random.py", line 275, in shuffle
  5. x[i], x[j] = x[j], x[i]
  6. TypeError: 'range' object does not support item assignment

部分源代码结构:

  1. x, y = range(height // 2), range(width)
  2. random.seed(height + width)
  3. random.shuffle(x)
  4. random.shuffle(y)

解决办法:
random. shuffle (object),其中object对象的类型必须是list的类型

所以将x,y的值强制转换为list类型

  1. x, y = list(range(height // 2)), list(range(width))
  2. random.seed(height + width)
  3. random.shuffle(x)
  4. random.shuffle(y)

提示:

因为,Python2和Python3中range()函数的用法不一致,在Python2中,输出的是list类型,而Python3中输出的是range类型。所以,需要强制转换!

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号