python中删除列表中指定的元素

 

1、

>>> a = ["aa", "xx", "mm", "xx", "xx", "kk", "aa"]
>>> a
['aa', 'xx', 'mm', 'xx', 'xx', 'kk', 'aa']
>>> a.count("xx")                         ## 统计指定元素的次数
3
>>> for i in range(a.count("xx")):        ## 利用for循环删除指定元素
...     a.remove("xx")
...
>>> a                                     ## 查看结果
['aa', 'mm', 'kk', 'aa']

 

posted @ 2022-05-26 23:25  小鲨鱼2018  阅读(150)  评论(0)    收藏  举报