Lyc*_*con 0 python indexing list
我试图从列表中删除一个项目; 但我试图删除的项目位于
索引之前index(' ').我怎么pop()能从这个列表中的项目.
您可以将pop索引作为其参数:
>>> lst = ['a', 'b', 'c', ' ', 'd']
>>> lst.pop(lst.index(' ') - 1)
'c'
>>> lst
['a', 'b', ' ', 'd']
Run Code Online (Sandbox Code Playgroud)
当然,您必须确保该项目' '之前的索引> = 1.如果您不需要删除的值,则可以使用del lst[index].