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

时间:2017-12-27 11:29:12

标签: python sage

我在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(&#39; bu&#39;),以便尝试检测并了解正在发生的事情。

编辑:

使用代码:

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发现这个!!我想我已经理解了我的错误!

1 个答案:

答案 0 :(得分:1)

请在“print('bu')”下方添加此声明“print(i,F)”,您可能会发现有什么不对的。