除了使用defaultdict,此代码看起来很好。列表似乎是更好的数据结构,因为:你的钥匙是顺序的
您在dict中存储一个唯一元素是另一个列表的列表
你可以做一件事:使用三元运算符(我不确定这是否是一种改进,但它将减少代码行数)
这是你的代码的修改版本,我有一些建议。在intervalx = xsize / xsegment #Get the size of the chunks
intervaly = ysize / ysegment #Get the size of the chunks
#Setup to segment the image storing the start values and key into a dictionary.
xstart = 0
ystart = 0
output = []
for y in xrange(0, ysize, intervaly):
numberofrows = intervaly if y + (intervaly * 2) < ysize else ysize -y
for x in xrange(0, xsize, intervalx):
numberofcolumns = intervalx if x + (intervalx * 2) < xsize else xsize -x
lst = [x, y, numberofcolumns, numberofrows]
output.append(lst)
#If it doesn't make any difference to your program, the above 2 lines could read:
#tple = (x, y, numberofcolumns, numberofrows)
#output.append(tple)
#This will be slightly more efficient
#(tuple creation is faster than list creation)
#and less memory hungry. In other words, if it doesn't need to be a list due
#to other constraints (e.g. you append to it later), you should make it a tuple.
现在要获取数据,可以执行offset_list=output[5]而不是{}