将嵌套的for循环的输出存储在数组中

时间:2019-09-23 04:26:09

标签: python arrays python-3.x list

        for x in range (1, 10)
           for y in range (1,10)
             for z in range (1,10)
             #In this loop, I perform some operations to generate an
             #output that has a shape (500,4). By using this output, I perform 
             #another set of operations as shown below)
              for i in range(1,500)
                 if i == 20
                    p = somevalue_1 #(which is derived by using columns in outpout(500,4)
                 if i == 40
                    q = somevalue_2 #(which is derived by using columns in outpout(500,4)
                 if i == 60
                    r = somevalue_3 #(which is derived by using columns in outpout(500,4)
                 if i == 80
                    s = somevalue_4 #(which is derived by using columns in outpout(500,4)

因此,对于1000次迭代(10x10x10),我想收集值p,q,r,s(shape(1000,4))。

1 个答案:

答案 0 :(得分:0)

您可以使用列表和范围(1,11)的列表来获取10个值:

allData = []
for x in range (1, 11):

    for y in range (1,11):

        for z in range (1,11):
            p=q=r=s = ''
            for i in range(1,500):

                if i == 20:
                    p = somevalue_1 #(which is derived by using columns in outpout(500,4)
                if i == 40:
                    q = somevalue_2 #(which is derived by using columns in outpout(500,4)
                if i == 60:
                    r = somevalue_3 #(which is derived by using columns in outpout(500,4)
                if i == 80:
                    s = somevalue_4 #(which is derived by using columns in outpout(500,4)
            temp =[p,q,r,s]
            allData.append(temp)  

这里的allData列表包含形状为1000,4的完整数据