python - 从数组中删除所有特定值

标签 python

我必须从数组中删除所有特定值(如果有的话),所以我写:

while value_to_remove in my_array:
    my_array.remove(value_to_remove)

是否有更多 pythonic 方式通过一个命令来做到这一点?

最佳答案

你可以试试:

过滤器(lambda a: a != value_to_remove, my_array)

例子:

>>> my_array = ["abc", "def", "xyz", "abc", "pop", "abc"]
>>> filter (lambda a: a != "abc", my_array)
['def', 'xyz', 'pop']

关于python - 从数组中删除所有特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13032448/

相关文章:

python - 在 Django 中使用多个时区

python - 在 Pandas 数据框中按列计算数字的出现次数

python - 从 bokeh==2.1.1 升级到 bokeh==2.2.0 会破坏 figure.image_rgba()

python - RFID应答卡没有唯一的atr

python - 在 Flask 中使用 bootstrap/css 模板时出现 HTTP 404 错误

python - Python 中的无服务器并发写访问

python - 如何在 Python 中将整个列表作为命令行参数传递?

python - 使用 True/False 作为键 - 这是如何/为什么起作用的?

python - 用给定字符串替换字符串中的字符

python - DataFrame 对象在计算长度时不可调用