【问题标题】:Elegant way to avoid repeating code after a loop避免循环后重复代码的优雅方法
【发布时间】:2020-05-15 03:36:18
【问题描述】:

我有一个问题,我已经跑了很多圈,但无法找到最佳解决方案,情况是:

1.我已经创建了一个效果很好的循环,那么我必须退出循环,因为我已经达到了我需要的结果。

2.我必须再次调用循环才能对我的第一个循环的结果进行其他操作,但我需要在前一个循环中创建的数组“index_val”,问题是为了让第二部分正常工作,我必须重复代码,(与我现在评论过的相同)。

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)

目前,我在屏幕上打印的内容如下:

 [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.]

你应该打印这个:

 [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.]

非常感谢您的帮助,请原谅我的英语不是我的母语,问候。

【问题讨论】:

    标签: python arrays python-3.x loops


    【解决方案1】:

    您可以只创建一个index_vals 列表,它将在运行第一个循环时保存所有 index_val。

    在第二个循环中,您可以访问它。

    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.]
    

    【讨论】:

      猜你喜欢
      • 2012-06-24
      • 2016-09-17
      • 1970-01-01
      • 2023-03-15
      • 2019-11-17
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 2014-03-07
      相关资源
      最近更新 更多