-
Given specific predicate def filter_chains(st: gemmi.Structure) -> None:
for model in st:
for chain_index, chain in enumerate(model):
if should_chain_be_deleted(chain):
(???)del model[chain_index]
break I suspect code above will not work because of model invalidation. |
Beta Was this translation helpful? Give feedback.
Answered by
wojdyr
Mar 27, 2021
Replies: 1 comment 8 replies
-
It's similar as it would be with deleting items from a list. Enumerating and deleting at the same time won't work. for i in range(len(somelist) - 1, -1, -1):
if some_condition(somelist, i):
del somelist[i] or you can first make a list of items that are to be deleted, and then delete them in reversed order: to_be_deleted = [n for n, chain in enumerate(model) if should_chain_be_deleted(chain)]
for index in reversed(to_be_deleted):
del model[index] |
Beta Was this translation helpful? Give feedback.
8 replies
Answer selected by
hlvlad
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's similar as it would be with deleting items from a list. Enumerating and deleting at the same time won't work.
But it works when going backward. You can either iterate backward (I'm using here example from SO):
or you can first make a list of items that are to be deleted, and then delete them in reversed order: