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

简介: 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 <module>
   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
  1. 索引范围检查: 在为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)
相关文章
|
6天前
|
存储 索引 Python
【Python】已解决:IndexError: list index out of range
【Python】已解决:IndexError: list index out of range
11 1
|
6天前
|
开发者 Python
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
8 0
|
26天前
|
Python
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
11 0
|
2月前
|
网络协议 API 开发者
Python 3.9 性能优化:更快的 list()、dict() 和 range() 等内置类型
Python 3.9 性能优化:更快的 list()、dict() 和 range() 等内置类型
22 1
|
索引 Python
Python:list列表迭代使用range和enumerate获取元素和索引
Python:list列表迭代使用range和enumerate获取元素和索引
157 1
|
19天前
|
安全 Java
java线程之List集合并发安全问题及解决方案
java线程之List集合并发安全问题及解决方案
18 1
|
14天前
|
存储 安全 Java
详解Java中集合的List接口实现的ArrayList方法 | Set接口实现的HashSet方法
详解Java中集合的List接口实现的ArrayList方法 | Set接口实现的HashSet方法
|
25天前
|
Java API
使用 Java 来实现两个 List 的差集操作
使用 Java 来实现两个 List 的差集操作
20 3
|
6天前
|
存储 Java 索引
Java List接口实现原理与性能评估
Java List接口实现原理与性能评估
|
12天前
|
存储 缓存 安全
Java List操作详解及常用方法
Java List操作详解及常用方法