python动态数组计算,计算数组偏移量的python方法

除了使用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]而不是{}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值