Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 元组列表删除错误_List_Python 3.x_Tuples - Fatal编程技术网

List 元组列表删除错误

List 元组列表删除错误,list,python-3.x,tuples,List,Python 3.x,Tuples,ExpenseL是我的元组列表,我试图从列表中从开始到停止删除元组,但我刚刚得到一个错误:在removeFromAtoB中 流行音乐(一) TypeError:“tuple”对象不能解释为整数 请帮帮我!!!:) def removeFromAtoB(): aux=复制。深度复制(expenseL) 打印(expenseL,“\n”) start=int(输入(“起始点:”) 停止=整数(输入(“结束点:”) j=0 对于expenseL中的我: 如果j>=start且j您正在迭代元组列表: f

ExpenseL是我的元组列表,我试图从列表中从开始停止删除元组,但我刚刚得到一个错误:在removeFromAtoB中 流行音乐(一) TypeError:“tuple”对象不能解释为整数 请帮帮我!!!:)

def removeFromAtoB():
aux=复制。深度复制(expenseL)
打印(expenseL,“\n”)
start=int(输入(“起始点:”)
停止=整数(输入(“结束点:”)
j=0
对于expenseL中的我:

如果j>=start且j您正在迭代元组列表:

for i in expenseL
这意味着
i
将是这些元组之一。然后尝试在
列表中使用它。pop

expenseL.pop(i)
这不起作用,因为
list.pop
需要一个索引。只需列举你的清单:

for index, tpl in enumerate(expenseL):
    ...
    expenseL.pop(index)
但这也会中断,因为删除元素时索引会发生变化。在这种情况下,您可以通过不增加
j
来避免这种情况,但更简单的方法是为切片分配一个空列表:

def removeFromAtoB():
    start = int(input("Starting point: "))
    stop = int(input("Ending point: "))
    expenseL[start:stop+1] = []
def removeFromAtoB():
    start = int(input("Starting point: "))
    stop = int(input("Ending point: "))
    expenseL[start:stop+1] = []