python多个for循环嵌套_优化python中的多个嵌套for循环

下面是测试代码,我的实际代码看起来几乎相似,其中我使用的原始矩阵是随机生成的。如何优化此嵌套for循环。我知道在python中是可能的,但是我不能这样做。在import time

import numpy as np

a = 1000

b = 500

sum2,sum3,sum4 = 0

t0 = time.time()

x = np.random.random(a*a).reshape([a,a])

for outer1 in xrange(0,a):

for inner1 in xrange(0,b):

for outer2 in xrange(0,a):

for inner2 in xrange(0, a):

sum2 += x[outer2][inner2] #this is not the only operation I have

for outer3 in xrange(0,a):

for inner3 in xrange(0, a):

sum3 += x[outer3][inner3] #this is not the only operation I have

for outer4 in xrange(0,a):

for inner4 in xrange(0, a):

sum4 += x[outer4][inner4] #this is not the only operation I have

print time.time() - t0

print 'sum2: '+str(sum2)+' sum3: '+str(sum3)+' sum4: '+str(sum4)

我使用的是python2.7。

谢谢您。在

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值