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

问题描述 投票:-2回答:1

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

python sage
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.