简体   繁体   中英

Elegant way to avoid repeating code after a loop

I have a problem to which I have given many laps without being able to find an optimal solution, the case is that:

1.I have created a loop that works very well, then I must get out of the loop because I have already achieved the results I need.

2.I have to call the loop again to do other operations with the results of my first loop but I need the array 'index_val', created in the previous loop, the problem is that for this second part to work well I have to repeat code, (the same one that I have now commented on).

import numpy as np

data = [[  7,     8,     9,    1,    2,     3],
        [ 10,    11,    12,    4,    5,    6],
        [  1,     2,     3,    4,    5,     6]]

index = []
Vector_master = np.zeros((12,1))  
Matrix_master = np.zeros((12,12))  

for i in range(len(data)):
    Vector_n = np.zeros((12,1)) 
    Matrix_n = np.zeros((12,12)) 

    index.append([data[i][0], data[i][1], data[i][2],
                    data[i][3], data[i][4], data[i][5]])   
    index_val = np.array([index[i]])
    index_val.shape = (index_val.size,)
    index_val -= 1
    print(index_val)

    w = data[i][0]*data[i][1]
    L = data[i][3]
    s_elemental = np.array([0,  w/2, w**2,  0,  w/2,  -w**2])    

    Vector_n[index_val, 0] = s_elemental
    for row in range(12):
        Vector_master[row]+=Vector_n[row] 

    matrix = np.array([[ L,      0,     0,   L,     0,      0],
                       [  0,   L**3,  L**2,   0,  L**3,   L**2],
                       [  0,   L**2,     L,   0,  L**2,      L],
                       [ -L,      0,     0,   L,     0,      0],
                       [  0,   L**3,  L**2,   0,  L**3,   L**2],
                       [  0,   L**2,     L,   0,  L**2,     L]])

    Matrix_n[np.ix_(index_val, index_val)] = matrix 
    for row in range(12):
        for col in range(12):
            Matrix_master[row][col] +=  Matrix_n[row][col] 

k11 = Matrix_master[ :6 , :6] 
Vector_cut = Vector_master[ :6]

vector_a = np.dot(k11, Vector_cut)  
Vector_b = np.zeros((6,1))   
vector_c = np.append(vector_a, Vector_b)  

print('\n')
for i in range(len(data)):
#    index.append([data[i][0], data[i][1], data[i][2],
#                    data[i][3], data[i][4], data[i][5]])   
#    index_val = np.array([index[i]])
#    index_val.shape = (index_val.size,)
#    index_val -= 1
    print('\n',index_val)
    vector_result = vector_c[index_val]
    print(vector_result)

At the moment, what I print on the screen is the following:

 [0 1 2 3 4 5]
[      0. -241439.  -62687.       0. -428416. -107104.]

 [0 1 2 3 4 5]
[      0. -241439.  -62687.       0. -428416. -107104.]

 [0 1 2 3 4 5]
[      0. -241439.  -62687.       0. -428416. -107104.]

You should print this:

 [6 7 8 0 1 2]
[0. 0. 0. 0. -241439. -62687.]

 [9 10 11 3 4 5]
[0. 0. 0. 0. -428416. -107104.]

 [0 1 2 3 4 5]
[0. -241439. -62687. 0. -428416. -107104.]

I really appreciate the help, excuse my English, it is not my native language, kind regards.

You can just create a index_vals list which will save all the index_val while running the first loop.

In the second loop, you can just access that.

import numpy as np

data = [[  7,     8,     9,    1,    2,     3],
        [ 10,    11,    12,    4,    5,    6],
        [  1,     2,     3,    4,    5,     6]]

index = []
Vector_master = np.zeros((12,1))  
Matrix_master = np.zeros((12,12))  

index_vals = [] # new list
for i in range(len(data)):
    Vector_n = np.zeros((12,1)) 
    Matrix_n = np.zeros((12,12)) 

    index.append([data[i][0], data[i][1], data[i][2],
                    data[i][3], data[i][4], data[i][5]])   
    index_val = np.array([index[i]])
    index_val.shape = (index_val.size,)
    index_val -= 1
    print(index_val)

    # appending
    index_vals.append(index_val)

    w = data[i][0]*data[i][1]
    L = data[i][3]
    s_elemental = np.array([0,  w/2, w**2,  0,  w/2,  -w**2])    

    Vector_n[index_val, 0] = s_elemental
    for row in range(12):
        Vector_master[row]+=Vector_n[row] 

    matrix = np.array([[ L,      0,     0,   L,     0,      0],
                       [  0,   L**3,  L**2,   0,  L**3,   L**2],
                       [  0,   L**2,     L,   0,  L**2,      L],
                       [ -L,      0,     0,   L,     0,      0],
                       [  0,   L**3,  L**2,   0,  L**3,   L**2],
                       [  0,   L**2,     L,   0,  L**2,     L]])

    Matrix_n[np.ix_(index_val, index_val)] = matrix 
    for row in range(12):
        for col in range(12):
            Matrix_master[row][col] +=  Matrix_n[row][col] 

k11 = Matrix_master[ :6 , :6] 
Vector_cut = Vector_master[ :6]

vector_a = np.dot(k11, Vector_cut)  
Vector_b = np.zeros((6,1))   
vector_c = np.append(vector_a, Vector_b)  

for i in range(len(data)):
    print('\n',index_vals[i])
    vector_result = vector_c[ index_vals[i] ]
    print(vector_result)
[6 7 8 0 1 2]
[ 9 10 11  3  4  5]
[0 1 2 3 4 5]

 [6 7 8 0 1 2]
[      0.       0.       0.       0. -241439.  -62687.]

 [ 9 10 11  3  4  5]
[      0.       0.       0.       0. -428416. -107104.]

 [0 1 2 3 4 5]
[      0. -241439.  -62687.       0. -428416. -107104.]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM