我想从i
和alist
删除索引blist
。是否有一种干净的Pythonic方法来实现这一目标?
答案 0 :(得分:1)
最直接的方式通常是最Pythonic:
for seq in (alist, blist):
del seq[i]
也就是说,如果您发现需要多次执行此操作,那么构建新列表通常比从同一列表中执行多次删除更有效:
# Slow O(n) operations where i > j > k
del seq[i]
del seq[j]
del seq[k]
# Better (one pass)
targets = {i, j, k}
seq[:] = [x for i, x in seq if i not in targets]