![](/img/trans.png)
[英]Write a function that removes the first occurrence of a string from another string.
[英]write a function that removes first occurence of a string
我想編寫一個函數remove
,該函數接受一個列表( word
)和一個字符串( words
)。 您的函數應消除word
中words
首次出現。 如果word
根本沒有出現words
,則該函數應僅使words
保持不變。
我努力了
def remove (item):
try:
words.remove('')
except ValueError:
pass
return words
你近了 您需要將''
文字替換為item
並添加words
參數。 由於是,它試圖從全局變量去掉一個空字words
,它也只是罰款。 但是,您可能希望它從傳遞的words
刪除item
。
另外,縮進也弄糟了(除了第一行之外的所有其他行都需要縮進一點),但是我想那不是您原始代碼的樣子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.