2016-08-25 23 views
0

值追加到列表的名單上有列出的幾個列表:如何使用列表的另一張單子,做數學運算

crt = [[80, 90, 6, 5.4, 8, 5], [65, 58, 2, 9.7, 1, 1], [83, 60, 4, 7.2, 4, 7], 
     [40, 80, 10, 7.5, 7, 10], [52, 72, 6, 2, 3, 8], [94, 96, 7, 3.6, 5, 6]] 

nc = [[1, 2, 3, 4, 5, 6], [1, 1, 1, 1, 1, 1], [-1, 1, -1, -1, -1, 1], 
     [2, 3, 5, 6, 1, 6], [10, 0, 0.5, 1, 0, 0], [0, 30, 5, 3, 0, 0], 
     [0, 0, 0, 0, 0, 5]] 

DivMatrix = [[[0, -15, 3, -40, -28, 14], [15, 0, 18, -25, -13, 29], 
       [-3, -18, 0, -43, -31, 11], [40, 25, 43, 0, 12, 54], 
       [28, 13, 31, -12, 0, 42], [-14, -29, -11, -54, -42, 0]], 
      [[0, 32, 30, 10, 18, -6], [-32, 0, -2, -22, -14, -38], 
       [-30, 2, 0, -20, -12, -36], [-10, 22, 20, 0, 8, -16], 
       [-18, 14, 12, -8, 0, -24], [6, 38, 36, 16, 24, 0]], 
       [[0, -4, -2, 4, 0, 1], [4, 0, 2, 8, 4, 5], 
       [2, -2, 0, 6, 2, 3], [-4, -8, -6, 0, -4, -3], 
       [0, -4, -2, 4, 0, 1], [-1, -5, -3, 3, -1, 0]], 
      [[-0.0, 4.299999999999999, 1.7999999999999998, 
       2.0999999999999996, -3.4000000000000004, -1.8000000000000003], 
       [-4.299999999999999, -0.0, -2.499999999999999, 
       -2.1999999999999993, -7.699999999999999, -6.1], 
       [-1.7999999999999998, 2.499999999999999, -0.0, 
       0.2999999999999998, -5.2, -3.6], [-2.0999999999999996, 
       2.1999999999999993, -0.2999999999999998, -0.0, -5.5, -3.9], 
       [3.4000000000000004, 7.699999999999999, 5.2, 5.5, 0, 1.6], 
       [1.8000000000000003, 6.1, 3.6, 3.9, -1.6, -0.0]], 
      [[0, -7, -4, -1, -5, -3], [7, 0, 3, 6, 2, 4], 
       [4, -3, 0, 3, -1, 1], [1, -6, -3, 0, -4, -2], 
       [5, -2, 1, 4, 0, 2], [3, -4, -1, 2, -2, 0]], 
      [[0, 4, -2, -5, -3, -1], [-4, 0, -6, -9, -7, -5], 
       [2, 6, 0, -3, -1, 1], [5, 9, 3, 0, 2, 4], [3, 7, 1, -2, 0, 2], 
       [1, 5, -1, -4, -2, 0]]] 

我想創建一個列表的列表與同尺寸DivMatrix。創建必須以for循環開始,然後使用6個不同的if語句,每個語句表示一個具有nc[3]的類型方程。我想創建第一個if語句,然後我將構建其他更復雜的語句。到目前爲止,我有這個算法,但它是錯誤的。

pref_indic = [] 
for x in range(len(crt)): 
    if (nc[3][x] == 1): 
     prolist = [] 
     for y in range(len(DivMatrix[x])): 
      prolist2 = [] 
      for z in range(len(DivMatrix[x])): 
       if (DivMatrix[y][z]<=0): 
        prolist2.append(0) 
       else: 
        prolist2.append(1) 
      prolist.append(prolist2) 
      pref_indic.append(prolist) 
    else: 
     print "wrong function type" 

print pref_indic 

如果這意味着如果類型是類型1的nc[3][x] = 1,那麼如果在DivMatrix[x]<=0值然後附加0,否則附加1.

因此,作爲結果,對於nc[3][4]其等於1,我會收到以下列表:

pref_indic[4] = [[0, 0, 0, 0, 0, 0], [1, 0, 1, 1, 1, 1], 
       [1, 0, 0, 1, 0, 1], [1, 0, 0, 0, 0, 0], 
       [1, 0, 1, 1, 0, 1], [1, 0, 0, 1, 0, 0]] 

對不起,由於文章太大,

+0

請修復'if(nc [3] [x] == 1)附近的縮進: prolist = []' –

+4

問題是什麼? –

+0

@h_e_u_r_e_k_a我想用6創建pref_indic,如果其中一個是我上面提到的那個,我想給出上面提到的結果。 pref_indic [4] = [[0,0,0,0,0,0],[1,0,1,1,1,1],[1,0,0,1,0,1],[1 ,0,0,0,0,0],[1,0,1,1,0,1],[1,0,0,1,0,0]] –

回答

0

這裏是你的代碼,修改就足以做你想做的(我猜,不知道):

pref_indic = [] 
for x in range(len(crt)): 
    if (nc[3][x] == 1): 
     prolist = [] 
     for y in range(len(DivMatrix[x])): 
      prolist2 = [] 
      for z in range(len(DivMatrix[x][y])): 
       if (DivMatrix[x][y][z]<=0): 
        prolist2.append(0) 
       else: 
        prolist2.append(1) 
      prolist.append(prolist2) 
     pref_indic.append(prolist) 
    else: 
     pref_indic.append("wrong function type") 

print pref_indic 

這裏現在是更多的東西可讀(但有限的,因爲我不「知道我在操作什麼樣的數據):

def process_eq_type_1(div_matrix_element): 
    prolist = [] 
    for element_of_element in div_matrix_element: 
     prolist2 = [] 
     for element_of_element_of_element in element_of_element: 
      if element_of_element_of_element <= 0: 
       prolist2.append(0) 
      else: 
       prolist2.append(1) 
     prolist.append(prolist2) 
    return prolist 


def process_eq_type_2(div_matrix_element): 
    return "eq type 2 not implemented" 


def process_eq_type_3(div_matrix_element): 
    return "eq type 3 not implemented" 


pref_indic = [] 
for eq_type, unclear_data in zip(nc[3], DivMatrix): 
    if (eq_type == 1): 
     pref_indic.append(process_eq_type_1(unclear_data)) 
    elif (eq_type == 2): 
     pref_indic.append(process_eq_type_2(unclear_data)) 
    elif (eq_type == 3): 
     pref_indic.append(process_eq_type_3(unclear_data)) 
    else: 
     pref_indic.append("wrong function type") 


print pref_indic 

它相當完全一樣的東西,但是:

  • 使用zip連結的兩個列表進行迭代,並直接檢索通緝值,而不是使用利用function對於每種情況下處理的索引
  • (只有:
    1. 主迴路是由簡化第一種過程的實現,但更多的功能已經存在)使用變量
  • 進程列表的程序「明確」的名字
  • 唱代碼對於其中NC [3] == 1是在功能提取出的情況下,提高了可讀性
  • 訪問由索引數據被儘可能流放
  • 這是更爲可讀和理解在我的意見(希望你會同意)。使用


    一些Python的技巧,你可以讓process_eq_type_1但更簡潔:

    def process_eq_type_1(div_matrix_element): 
        prolist = [] 
        for element_of_element in div_matrix_element: 
         prolist2 = [] 
         for element_of_element_of_element in element_of_element: 
          prolist2.append(int(element_of_element_of_element > 0)) 
         prolist.append(prolist2) 
        return prolist 
    

    或使用list comprehension

    def process_eq_type_1(div_matrix_element): 
        prolist = [] 
        for element_of_element in div_matrix_element: 
         prolist.append([int(element_of_element_of_element > 0) 
             for element_of_element_of_element 
             in element_of_element]) 
        return prolist 
    

    最後的忠告:你可能想閱讀the Python tutorial

    +0

    這將是下一步!我想先創建一個for循環與6 if,然後將其轉換爲6個不同的功能! :) 真的很感謝你!是的,這正是我想要做的! –

    0

    這是回答您的問題嗎?

    pref_indic = [] 
    for x in range(len(crt)): 
        if nc[3][x] == 1: 
         prolist = [] 
         for y in range(len(DivMatrix[x])): 
          prolist2 = [] 
          for z in range(len(DivMatrix[x][y])): 
           if DivMatrix[x][y][z] <= 0: 
            prolist2.append(0) 
           else: 
            prolist2.append(1) 
          prolist.append(prolist2) 
          pref_indic.append(prolist) 
        elif nc[3][x] == 2: 
         # add code here 
        elif nc[3][x] == 3: 
         # add code here 
        elif nc[3][x] == 4: 
         # add code here 
        # ...etc 
        else: 
         print "wrong function type" 
    
    print pref_indic 
    
    +0

    YEEEEEES,正是我想要的!非常感謝:) –

    相關問題