簡體   English   中英

我錯過了什么,因為我不斷收到“類型錯誤:列表索引必須是整數或切片,而不是列表”

[英]What am I missing because I keep getting a, "TypeError: list indices must be integers or slices, not list"

我目前正在嘗試在 python3 中逐項執行 for 循環。 我可能會錯過什么?

def add_rows(sq_list):
    rowNum = [[0, 0, 0],
              [0, 0, 0],
              [0, 0, 0]]
    
    for row in rowNum:
        total = 0
        for column in sq_list[row]:
            total += column
            
        print("Row total is:", total)
    
add_rows([[1, 3, 8],
          [2, 4, 6],
          [7, 9, 0]])

當您遍歷行時,行是整個列表。 所以你的第二次迭代應該在單個元素上。 所以它應該是這樣的(例如):

for row in rowNum:
    total = 0
    for element in row:
        total += element
    
    print(...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM