Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何使用python在不知道索引的情况下删除列表中的元素_Python 3.x_List - Fatal编程技术网

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']