计算机视觉编程第7章 7.6.2图像搜索演示程序中
class SearchDemo:
def __init__(self):
# load list of images
f = open('webimlist.txt')
self.imlist = f.readlines()
f.close()
self.nbr_images = len(self.imlist)
self.ndx = range(self.nbr_images)
本书程序使用的Python是2.6版本,在Python3.x版本中,程序会出现如下错误:
random.shuffle(self.ndx)
File "mtrand.pyx", line 4832, in mtrand.RandomState.shuffle
File "mtrand.pyx", line 4835, in mtrand.RandomState.shuffle
TypeError: 'range' object does not support item assignment
原因是在Python2.6版本中,range()是list类型的
而在Python3.x版本中,range()是range类型
所以需要将:
self.ndx = range(self.nbr_images) 改为:
self.ndx = list(range(self.nbr_images)
程序就能正常运行了。