IndexError:使用嵌套for循环列表索引超出范围

我正在编写CoCalc(Sage,Python 2.7中添加了一些附加组件),因为它执行非常简单的事情,我遇到了麻烦。我必须承认我不是一个频繁的程序员,所以可能我错过了一些相当明显的东西。IndexError:使用嵌套for循环列表索引超出范围

将问题置于上下文中。 max_faces_list是集合的列表。我想从列表中删除其他元素子集的元素。

的代码是:

for i in range(0,len(max_faces_list)-2): 

i

for j in range(i+1,len(max_faces_list)):

j

if max_faces_list[i].issubset(max_faces_list[j]):

print('bu')

F.remove(F[i])

break

的输出是:

0 

1

2

3

4

bu

1

2

3

4

bu

2

3

4

5

bu

3

4

5

6

bu

4

5

6

7

8

9

10

bu

5

6

7

8

9

10

bu

6

7

8

9

10

11

bu

7

8

9

10

bu

Error in lines 1-8

Traceback (most recent call last):

File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute

exec compile(block+'\n', '', 'single') in namespace, locals

File "", line 7, in <module>

IndexError: list index out of range

在情况下,它是有帮助的:

max_faces_list=[set([(0, 2, 3)]), set([(0, 1, 2)]), set([(1, 2, 3)]), set([(0, 1, 3)]), set([(0, 2, 3), (0, 1, 2)]), set([(0, 2, 3), (1, 2, 3)]), set([(0, 2, 3), (0, 1, 3)]), set([(0, 1, 2), (1, 2, 3)]), set([(0, 1, 2), (0, 1, 3)]), set([(1, 2, 3), (0, 1, 3)]), set([(0, 2, 3), (0, 1, 2), (1, 2, 3)]), set([(0, 2, 3), (0, 1, 2), (0, 1, 3)]), set([(0, 2, 3), (1, 2, 3), (0, 1, 3)]), set([(0, 1, 2), (1, 2, 3), (0, 1, 3)])] 

F=[Simplicial complex with vertex set (0, 2, 3) and facets {(0, 2, 3)}, Simplicial complex with vertex set (0, 1, 2) and facets {(0, 1, 2)}, Simplicial complex with vertex set (1, 2, 3) and facets {(1, 2, 3)}, Simplicial complex with vertex set (0, 1, 3) and facets {(0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3), (0, 1, 3)}]

我已经介绍了一些代码(i,j和打印('bu')以试图检测和理解正在发生的事情。

编辑:

随着代码:

for i in range(0,len(max_faces_list)-2): 

i

for j in range(i+1,len(max_faces_list)):

j

if max_faces_list[i].issubset(max_faces_list[j]):

print('bu')

print(i, F)

F.remove(F[i])

break

的输出是:

0 

1

2

3

4

bu

(0, [Simplicial complex with vertex set (0, 2, 3) and facets {(0, 2, 3)}, Simplicial complex with vertex set (0, 1, 2) and facets {(0, 1, 2)}, Simplicial complex with vertex set (1, 2, 3) and facets {(1, 2, 3)}, Simplicial complex with vertex set (0, 1, 3) and facets {(0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3), (0, 1, 3)}])

1

2

3

4

bu

(1, [Simplicial complex with vertex set (0, 1, 2) and facets {(0, 1, 2)}, Simplicial complex with vertex set (1, 2, 3) and facets {(1, 2, 3)}, Simplicial complex with vertex set (0, 1, 3) and facets {(0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3), (0, 1, 3)}])

2

3

4

5

bu

(2, [Simplicial complex with vertex set (0, 1, 2) and facets {(0, 1, 2)}, Simplicial complex with vertex set (0, 1, 3) and facets {(0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3), (0, 1, 3)}])

3

4

5

6

bu

(3, [Simplicial complex with vertex set (0, 1, 2) and facets {(0, 1, 2)}, Simplicial complex with vertex set (0, 1, 3) and facets {(0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3), (0, 1, 3)}])

4

5

6

7

8

9

10

bu

(4, [Simplicial complex with vertex set (0, 1, 2) and facets {(0, 1, 2)}, Simplicial complex with vertex set (0, 1, 3) and facets {(0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3), (0, 1, 3)}])

5

6

7

8

9

10

bu

(5, [Simplicial complex with vertex set (0, 1, 2) and facets {(0, 1, 2)}, Simplicial complex with vertex set (0, 1, 3) and facets {(0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3), (0, 1, 3)}])

6

7

8

9

10

11

bu

(6, [Simplicial complex with vertex set (0, 1, 2) and facets {(0, 1, 2)}, Simplicial complex with vertex set (0, 1, 3) and facets {(0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3), (0, 1, 3)}])

7

8

9

10

bu

(7, [Simplicial complex with vertex set (0, 1, 2) and facets {(0, 1, 2)}, Simplicial complex with vertex set (0, 1, 3) and facets {(0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(1, 2, 3), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 2, 3), (0, 1, 2), (0, 1, 3)}, Simplicial complex with vertex set (0, 1, 2, 3) and facets {(0, 1, 2), (1, 2, 3), (0, 1, 3)}])

Error in lines 2-10

Traceback (most recent call last):

File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1013, in execute

exec compile(block+'\n', '', 'single') in namespace, locals

File "", line 8, in <module>

IndexError: list index out of range

所以该错误是,我不再可以删除F [i]中。感谢@Akash Wankhede发现这个!我想我明白了我的错误!

回答:

请在print('bu')“的下方添加”print(i,F)“语句,您可能会发现什么是错误的。

以上是 IndexError:使用嵌套for循环列表索引超出范围 的全部内容, 来源链接: utcz.com/qa/264605.html

回到顶部