我有一个字符列表和索引列表
myList = ['a','b','c','d']
toRemove = [0,2]
我想在一次操作中得到这个
myList = ['b','d']
我可以做到这一点,但有没有办法更快地做到这一点?
toRemove.reverse()
for i in toRemove:
myList.pop(i)
最佳答案
简洁的回答
>>> myList = ['a','b','c','d']
>>> toRemove = [0,2]
>>>
>>> [v for i, v in enumerate(myList) if i not in toRemove]
['b', 'd']
>>>
关于python - 在 Python 中按索引从列表中删除元素的简洁方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10940534/