如何在不使用.remove的情况下删除列表中的值?

时间:2014-10-30 03:34:33

标签: python list

我有

x = 1

list = [1,2,3]

我想通过引用x的而不是索引来删除列表中的1 - 而不使用list.remove(x)。这怎么可能?

3 个答案:

答案 0 :(得分:1)

确实,有很多方法可以做到这一点。

x = 5
my_list = [1, 2, 3, 5, 6, 9, 4]

del my_list[my_list.index(x)]

my_list.pop(my_list.index(x))

答案 1 :(得分:1)

x = 1
list = [1,2,3]
list = [i for i in list if i != x]

答案 2 :(得分:0)

list = [1,2,3]
list = [i for i in list if i is not 1]