IndexError: list index out of range using nested for loops

IndexError: list index out of range using nested for loops

我在 CoCalc(Sage,它是 Python 2.7,添加了一些额外功能)中编程,只是为了做一些非常简单的事情,但我遇到了麻烦。我必须承认我不是一个经常编程的人,所以我可能遗漏了一些应该很明显的东西。

将问题放在上下文中。 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 和 print('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)",您可能会发现发生了什么问题。