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