2016-12-03 55 views
1

我正在製作一個基於文本的冒險遊戲,並且有點麻煩。如何從列表中刪除一個元素,當我不知道它是索引號

我想從清單列表中刪除一個項目,但是它的索引是未知的,因爲用戶可能在我嘗試刪除之前拾取其他項目。

我該如何去刪除它?

名單是:

inventory = ["sword", "healing potion"] 

,當你去通過遊戲中你拾取物品,它被添加到這個列表。

回答

0

上面的回答是最好的一個。另一種方式來做到這一點是(也許這可能是太有用):

index = inventory.index("sword") #in this way you get the index del(inventory[index]) #and now remove it

+0

或'inventory.pop(指數)' – Bodhi94

4

你可以簡單的使用方法remove

inventory.remove('sword') 
相關問題