Python while循环中的列表值始终超出范围

Python while循环中的列表值始终超出范围,python,while-loop,index-error,Python,While Loop,Index Error,我正在尝试将重叠的间隔合并到一个列表中,以创建一个只有不重叠间隔的较小列表。然而,我经常在if语句中得到一个索引器:list索引超出范围 我尝试过使用for循环,但这似乎是一个倒退,因为for循环语句中的列表长度是静态的 list = [(13,15),(-1,16),(12,17),(-5,-2),(2,5)] x = 0 while x < len(list): if list[x][0] < list[x+1][0] and list[x][1] > list[x

我正在尝试将重叠的间隔合并到一个列表中,以创建一个只有不重叠间隔的较小列表。然而,我经常在if语句中得到一个索引器:list索引超出范围

我尝试过使用for循环,但这似乎是一个倒退,因为for循环语句中的列表长度是静态的

list = [(13,15),(-1,16),(12,17),(-5,-2),(2,5)]
x = 0
while x < len(list):
    if list[x][0] < list[x+1][0] and list[x][1] > list[x+1][1]:
        del list[x+1]
    if list[x][0] > list[x+1][0] and list[x][1] > list[x+1][1]:
        tuple2 = (list[x+1][0], list[x][1])
        list.append(tuple2)
        del list[x]
        del list[x+1]
    if list[x][0] < list[x+1][0] and list[x][1] < list[x+1][1]:
        tuple2 = (list[x][0], list[x+1][1])
        list.append(tuple2)
        del list[x]
        del list[x+1]
    x = x + 1

print(list)
list=[(13,15),(-1,16),(12,17),(-5,-2),(2,5)]
x=0
当x列表[x+1][1]:
删除列表[x+1]
如果列表[x][0]>列表[x+1][0]和列表[x][1]>列表[x+1][1]:
tuple2=(列表[x+1][0],列表[x][1])
list.append(tuple2)
删除列表[x]
删除列表[x+1]
如果列表[x][0]<列表[x+1][0]和列表[x][1]<列表[x+1][1]:
tuple2=(列表[x][0],列表[x+1][1])
list.append(tuple2)
删除列表[x]
删除列表[x+1]
x=x+1
打印(列表)

预期的输出是:
[(-5,-2),(-1,17)]

假设您坚持使用变量名
list
(您应该更改),您只能迭代到该列表的长度-1,因为您在循环中使用
x+1

x = 0
while x < (len(list)-1):
    if list[x][0] < list[x+1][0] and list[x][1] > list[x+1][1]:
        del list[x+1]
    if list[x][0] > list[x+1][0] and list[x][1] > list[x+1][1]:
        tuple2 = (list[x+1][0], list[x][1])
        list.append(tuple2)
        del list[x]
        del list[x+1]
    if list[x][0] < list[x+1][0] and list[x][1] < list[x+1][1]:
        tuple2 = (list[x][0], list[x+1][1])
        list.append(tuple2)
        del list[x]
        del list[x+1]
    x = x + 1

print(list)
x=0
当x<(列(列表)-1时:
如果列表[x][0]<列表[x+1][0]和列表[x][1]>列表[x+1][1]:
删除列表[x+1]
如果列表[x][0]>列表[x+1][0]和列表[x][1]>列表[x+1][1]:
tuple2=(列表[x+1][0],列表[x][1])
list.append(tuple2)
删除列表[x]
删除列表[x+1]
如果列表[x][0]<列表[x+1][0]和列表[x][1]<列表[x+1][1]:
tuple2=(列表[x][0],列表[x+1][1])
list.append(tuple2)
删除列表[x]
删除列表[x+1]
x=x+1
打印(列表)

假设您坚持使用变量名
list
(您应该更改该变量名),您只能迭代到该列表的长度-1,因为您在循环中使用
x+1
来处理它:

x = 0
while x < (len(list)-1):
    if list[x][0] < list[x+1][0] and list[x][1] > list[x+1][1]:
        del list[x+1]
    if list[x][0] > list[x+1][0] and list[x][1] > list[x+1][1]:
        tuple2 = (list[x+1][0], list[x][1])
        list.append(tuple2)
        del list[x]
        del list[x+1]
    if list[x][0] < list[x+1][0] and list[x][1] < list[x+1][1]:
        tuple2 = (list[x][0], list[x+1][1])
        list.append(tuple2)
        del list[x]
        del list[x+1]
    x = x + 1

print(list)
x=0
当x<(列(列表)-1时:
如果列表[x][0]<列表[x+1][0]和列表[x][1]>列表[x+1][1]:
删除列表[x+1]
如果列表[x][0]>列表[x+1][0]和列表[x][1]>列表[x+1][1]:
tuple2=(列表[x+1][0],列表[x][1])
list.append(tuple2)
删除列表[x]
删除列表[x+1]
如果列表[x][0]<列表[x+1][0]和列表[x][1]<列表[x+1][1]:
tuple2=(列表[x][0],列表[x+1][1])
list.append(tuple2)
删除列表[x]
删除列表[x+1]
x=x+1
打印(列表)

只有
x
才能运行此代码。它需要是
x

list = [(13,15),(-1,16),(12,17),(-5,-2),(2,5)]
x = 0
while x < len(list) -1 :
    print(x)
    if list[x][0] < list[x+1][0] and list[x][1] > list[x+1][1]:
        del list[x+1]
    if list[x][0] > list[x+1][0] and list[x][1] > list[x+1][1]:
        tuple2 = (list[x+1][0], list[x][1])
        list.append(tuple2)
        del list[x]
        del list[x+1]
    if list[x][0] < list[x+1][0] and list[x][1] < list[x+1][1]:
        tuple2 = (list[x][0], list[x+1][1])
        list.append(tuple2)
        del list[x]
        del list[x+1]
    x = x + 1

print(list)
list=[(13,15),(-1,16),(12,17),(-5,-2),(2,5)]
x=0
当x列表[x+1][1]:
删除列表[x+1]
如果列表[x][0]>列表[x+1][0]和列表[x][1]>列表[x+1][1]:
tuple2=(列表[x+1][0],列表[x][1])
list.append(tuple2)
删除列表[x]
删除列表[x+1]
如果列表[x][0]<列表[x+1][0]和列表[x][1]<列表[x+1][1]:
tuple2=(列表[x][0],列表[x+1][1])
list.append(tuple2)
删除列表[x]
删除列表[x+1]
x=x+1
打印(列表)

只有
x
才能运行此代码。它需要是
x

list = [(13,15),(-1,16),(12,17),(-5,-2),(2,5)]
x = 0
while x < len(list) -1 :
    print(x)
    if list[x][0] < list[x+1][0] and list[x][1] > list[x+1][1]:
        del list[x+1]
    if list[x][0] > list[x+1][0] and list[x][1] > list[x+1][1]:
        tuple2 = (list[x+1][0], list[x][1])
        list.append(tuple2)
        del list[x]
        del list[x+1]
    if list[x][0] < list[x+1][0] and list[x][1] < list[x+1][1]:
        tuple2 = (list[x][0], list[x+1][1])
        list.append(tuple2)
        del list[x]
        del list[x+1]
    x = x + 1

print(list)
list=[(13,15),(-1,16),(12,17),(-5,-2),(2,5)]
x=0
当x列表[x+1][1]:
删除列表[x+1]
如果列表[x][0]>列表[x+1][0]和列表[x][1]>列表[x+1][1]:
tuple2=(列表[x+1][0],列表[x][1])
list.append(tuple2)
删除列表[x]
删除列表[x+1]
如果列表[x][0]<列表[x+1][0]和列表[x][1]<列表[x+1][1]:
tuple2=(列表[x][0],列表[x+1][1])
list.append(tuple2)
删除列表[x]
删除列表[x+1]
x=x+1
打印(列表)

你能提供你的全部代码吗。我有点困惑,因为
list
是Python中的一个函数,我不确定变量
x
是什么。1)不要使用
list
作为变量名,它会隐藏内置的
list
类型,并会导致奇怪的错误。2) 此代码中有很多
x+1
s,但是
x
可以进入
len(list)-1
;换句话说,您尝试访问的最大索引是
len(list)-1+1
,这总是超出范围。3) 在迭代列表时向列表中添加项目或从列表中删除项目会导致灾难。通常解决方案是对列表的副本进行操作。列表是我列表的名称,但我只是将变量名更改为list1。如果x+1导致列表越界,我如何在迭代时调用列表中的下一个元素?当x==len(list)-1使用x+1的诅咒将导致错误,您在最后一个元素都准备好了。只是好奇。如果删除索引
x
,列表的内容不会上移吗?然后在
x
之后删除
x+1
是否正确?能否提供您的全部代码。我有点困惑,因为
list
是Python中的一个函数,我不确定变量
x
是什么。1)不要使用
list
作为变量名,它会隐藏内置的
list
类型,并会导致奇怪的错误。2) 此代码中有很多
x+1
s,但是
x
可以进入
len(list)-1
;换句话说,您尝试访问的最大索引是
len(list)-1+1
,这总是超出范围。3) 在迭代列表时向列表中添加项目或从列表中删除项目会导致灾难。通常解决方案是对列表的副本进行操作。列表是我列表的名称,但我只是将变量名更改为list1。如果x+1导致列表越界,我如何在迭代时调用列表中的下一个元素?当x==len(list)-1使用x+1的诅咒将导致错误,您在最后一个元素都准备好了。Ju