- 使用列表推导式:
numbers = [1, 2, 3, 4, 5]
removed_number = 3
numbers = [x for x in numbers if x != removed_number]
print(numbers) # 输出: [1, 2, 4, 5]
- 使用filter()函数:
numbers = [1, 2, 3, 4, 5]
removed_number = 3
numbers = list(filter(lambda x: x != removed_number, numbers))
print(numbers) # 输出: [1, 2, 4, 5]
- 使用remove()方法:
numbers = [1, 2, 3, 4, 5]
removed_number = 3
numbers.remove(removed_number)
print(numbers) # 输出: [1, 2, 4, 5]
- 使用del语句:
numbers = [1, 2, 3, 4, 5]
removed_index = 2
del numbers[removed_index]
print(numbers) # 输出: [1, 2, 4, 5]
标签:输出,index,python,number,numbers,移除,print,removed,解法
From: https://blog.51cto.com/lzning/9156757