row[i] = col[j] = TrueIndexError: list assignment index out of range

Traceback (most recent call last):
  File "C:/Users/PycharmProjects/pySpider/字典/矩阵置零.py", line 26, in
    row[i] = col[j] = True
IndexError: list assignment index out of range

你遇到的错误,“IndexError: list assignment index out of range(索引错误:列表分配索引超出范围)”,表示你正试图为列表中超出实际大小的索引赋值。

在你的具体代码片段中,似乎你正试图在列表rowcol的索引ij处赋值。然而,很可能这些索引超出了列表的实际长度,导致了 IndexError。

要解决此问题,你需要确保索引ij在列表rowcol的有效范围内。以下是一些你可以检查的事项:

  1. 列表初始化: 确保在尝试为特定索引赋值之前,已使用适当的大小初始化了rowcol列表。

     例如,使用零进行初始化
    
    
    row = [0] * some_size
    col = [0] * some_size
    

  2. 索引范围检查: 在为row[i]col[j]赋值之前,请确保ij在列表的有效范围内。你可以添加条件检查来确保这一点。

    if 0 <= i < len(row) and 0 <= j < len(col):
        row[i] = col[j] = True
    else:
        print("Invalid indices: i =", i, "j =", j)
    

你可能感兴趣的:(力扣,前端)