2016-10-05 65 views
0

我正在使用python。這個任務很簡單,我理解這個概念,我對計算機編程毫無知識,所以請溫和一些,並且儘量詳細地解釋一切,就好像我什麼都不知道,我正在努力學習。這表明用所有組合ijk,ikj,jik,jki,kij和kji返回相同的結果。下一步是用4000做同樣的事情(試着想象如何把4000放在矩陣中,拍攝自己。) 我知道有些東西像numpy,但我不知道如何使用它,但是我打算嘗試從基礎開始,直接從Windows終端。我想感謝你的時間,並幫助這裏的傢伙!謝謝!在這裏輸入嵌套循環全部

enter code here 

    X = [[1,2,3,4,5], 
     [6,7,8,9,10]] 



    Y = [[1,2], 
     [3,4], 
     [5,6], 
     [7,8], 
     [9,10]] 



    result = [[0,0], 
      [0,0]] 
    empty = [[0,0], 
      [0,0]] 

     for i in range(len(X)): 
      for j in range(len(Y[0])): 
        for k in range(len(Y)): 
         result[i][j] += X[i][k] * Y[k][j] 
for r in result: 
    print r 
    result = empty 
    del r[:] 


print"" 

for i in range(len(X)): 
    for k in range(len(Y)): 
     for j in range(len(Y[0])): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
    del r[:] 


print"" 

for i in range(len(X)): 
    for k in range(len(Y)): 
     for j in range(len(Y[0])): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
    result = empty 
    del r[:] 
    del range[:] 

print"" 

for j in range(len(Y[0])):  
    for i in range(len(X)): 
     for k in range(len(Y)): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
    result = empty 
    del r[:] 

for j in range(len(Y[0])): 
    for k in range(len(Y)): 
     for i in range(len(X)): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
    result = empty 
    del r[:] 

for k in range(len(Y)): 
    for i in range(len(X)): 
     for j in range(len(Y[0])): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
    result = empty 
    del r[:] 

for k in range(len(Y)): 
    for j in range(len(Y[0])): 
     for i in range(len(X)): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
    result = empty 
    del r[:] 

問題代碼,是該指數,爲第3集運行代碼的時候,沒有與該指數的一個問題,我不明白這一點。

  result[i][j] += X[i][k] * Y[k][j] 
     IndexError: list index out of range 
+1

歡迎來到Stackoverflow!一般來說,當你發佈一個問題時,你想要堅持一個MCVE示例(http://stackoverflow.com/help/mcve),以便我們不必通讀如此長的代碼塊。這對於你收到的答案的時間表和質量肯定有幫助 –

+0

當你使用'row','col'等等而不是'i','j'等名字時更容易 – furas

+0

print'i','' j','k'和'​​result [i] [j]','X [i] [k]','Y [k] [j]'來查看哪個元素產生問題。 – furas

回答

0
for i in range(len(X)): 
    for k in range(len(Y)): 
     for j in range(len(Y[0])): 
      result[i][j] += X[i][k] * Y[k][j] 

result[i][j]2x2矩陣len(X)len(Y)均爲2。同時k一路高達5。這是什麼給你你的list index out of range錯誤

你必須使你的矩陣大小匹配。

+0

我將如何更改我的代碼以使其具有這種功能,以便我可以輕鬆切換ijk。 – Khalil

0

我打印ijkresult[i][j]X[i][k]Y[k][j]在第三組發現:

你忘result = empty

但還有另一個問題:result = empty不重複名單,而是將相同列表爲兩個變量。因此,當您刪除r[:]時,您將刪除resultempty,並獲得空列表[]並且甚至不存在result[0][0]

您需要result = empty[:]才能複製empty並指定爲result

X = [[1,2,3,4,5], 
    [6,7,8,9,10]] 

Y = [[1,2], 
    [3,4], 
    [5,6], 
    [7,8], 
    [9,10]] 

result = [[0,0], 
     [0,0]] 

empty = [[0,0], 
     [0,0]] 


for i in range(len(X)): 
    for j in range(len(Y[0])): 
      for k in range(len(Y)): 
       result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
print 

result = empty[:] 

for i in range(len(X)): 
    for k in range(len(Y)): 
     for j in range(len(Y[0])): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
print 

result = empty[:] 

for i in range(len(X)): 
    for k in range(len(Y)): 
     for j in range(len(Y[0])): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
print 

result = empty[:] 

for j in range(len(Y[0])):  
    for i in range(len(X)): 
     for k in range(len(Y)): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
print 

result = empty[:] 

for j in range(len(Y[0])): 
    for k in range(len(Y)): 
     for i in range(len(X)): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
print 

result = empty[:] 

for k in range(len(Y)): 
    for i in range(len(X)): 
     for j in range(len(Y[0])): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
print 

result = empty[:] 

for k in range(len(Y)): 
    for j in range(len(Y[0])): 
     for i in range(len(X)): 
      result[i][j] += X[i][k] * Y[k][j] 

for r in result: 
    print r 
print 

result = empty[:] 
+0

我明白了結果= empty_list []:] – Khalil