Python 3.x 如何使用python在不知道索引的情况下删除列表中的元素
这里我有一个列表,它包含许多重复的元素。我想删除列表中第二个重复的元素。如何删除列表中的特定元素Python 3.x 如何使用python在不知道索引的情况下删除列表中的元素,python-3.x,list,Python 3.x,List,这里我有一个列表,它包含许多重复的元素。我想删除列表中第二个重复的元素。如何删除列表中的特定元素 l=["a","b","a","a","a"] here i want to remove "a" after the second element of b(i.e a[3]) without using the index how can I remove the element in the list. 您可以在列表中搜索b的索引,然后将其作为搜索a索引的起点 l=[“a”、“b”、“a
l=["a","b","a","a","a"]
here i want to remove "a" after the second element of b(i.e a[3])
without using the index how can I remove the element in the list.
您可以在列表中搜索b的索引,然后将其作为搜索a索引的起点
l=[“a”、“b”、“a”、“a”、“a”]
b_指数=l指数(“b”)
打印(“b索引为”,b_索引)
在b=l.index(“a”,b_索引)之后的第一个a_
打印(“b之后的第一个a在索引中”,b之后的第一个a)
del l[先a后b]
印刷品(l)
输出
b index is 1
first a after b is at index 2
['a', 'b', 'a', 'a']
Python
list.index
采用start
参数,我们可以使用该参数在第一次出现b
之后查找a
l = ["a","b","a","a","a"]
l.pop(l.index('a', l.index('b')+1))
# l = ['a', 'b', 'a', 'a']