Python 3执行python2代码时遇到的部分问题

1.TypeError: 'range' object does not support item assignment

报错语句:

np.random.shuffle(keys)

此处的keys是由range() 函数产生(在此没有贴出全部的代码)。在Python2中,range返回的是列表。而Python3 range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型。代码需要修改为:

keys = list(range(num_obj))
np.random.shuffle(keys)


2.TypeError: 'range' object does not support item assignment

报错语句:

train_img, train_label = train_gen.next()

需要把内置函数.next()修改为.__next__()。修改后上述语句为:

train_img, train_label = train_gen.__next__()



未完,待续。。。


猜你喜欢

转载自blog.csdn.net/breeze5428/article/details/80212118
今日推荐