从列表中删除所有出现的值?用Pythonremove()将删除列表中值的第一次出现。如何删除列表中所有出现的值,而不对列表进行排序?我就是这么想的。>>> x = [1, 2, 3, 4, 2, 2, 3]>>> def remove_values_from_list(the_list, val):
while val in the_list:
the_list.remove(val)>>> remove_values_from_list(x, 2)>>> x[1, 3, 4, 3]
3 回答

繁花不似锦
TA贡献1851条经验 获得超4个赞
2.x
>>> x = [1,2,3,2,2,2,3,4]>>> filter(lambda a: a != 2, x)[1, 3, 3, 4]
3.x
>>> x = [1,2,3,2,2,2,3,4]>>> list(filter((2).__ne__, x))[1, 3, 3, 4]
>>> x = [1,2,3,2,2,2,3,4]>>> list(filter(lambda a: a != 2, x))[1, 3, 3, 4]

一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
def remove_values_from_list(the_list, val): return [value for value in the_list if value != val]x = [1, 2, 3, 4, 2, 2, 3]x = remove_values_from_list(x, 2)print x# [1, 3, 4, 3]
添加回答
举报
0/150
提交
取消