Remove all occurrences of a value from a list?

后端 未结 23 2399
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 23:45

In Python remove() will remove the first occurrence of value in a list.

How to remove all occurrences of a value from a list?

This is w

23条回答
  •  滥情空心
    2020-11-21 23:57

    I believe this is probably faster than any other way if you don't care about the lists order, if you do take care about the final order store the indexes from the original and resort by that.

    category_ids.sort()
    ones_last_index = category_ids.count('1')
    del category_ids[0:ones_last_index]
    

提交回复
热议问题