eg:輸入:
[1, 2, 3, 4, 3, 5, 3]
刪除:3
結果: [1, 2, 4, 5]
def shan(lt, item):
count = lt.count(item)
if count == 0:
print('沒有元素,不能刪除')
return
for i in range(count):
lt.remove(item)
return lt
print(shan([1, 2, 3, 4, 3, 5, 3],3))
eg:輸入:
[1, 2, 3, 4, 3, 5, 3]
刪除:3
結果: [1, 2, 4, 5]
def shan(lt, item):
count = lt.count(item)
if count == 0:
print('沒有元素,不能刪除')
return
for i in range(count):
lt.remove(item)
return lt
print(shan([1, 2, 3, 4, 3, 5, 3],3))
1.while循環的格式 while 條件: 條件滿足時,做的事情1 條件滿足時,做的事情2 條件滿足時,做的事情3 ...(省略)... demo