有沒有一種方法可以刪除列表中某個范圍內的項目?例如:a = ['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']如何從中刪除項目'jumped'到最后一個項目?我事先知道,'jumped'在我的列表中只會出現一次。
2 回答

有只小跳蛙
TA貢獻1824條經驗 獲得超8個贊
假設您知道該字符串僅出現一次,則可以使用它list.index,然后將其與列表切片一起使用:
a = ['the', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog']
idx = a.index('jumped')
res = a[:idx]
print(res)
['the', 'quick', 'brown', 'fox']
添加回答
舉報
0/150
提交
取消