blocks , exclude. , exclude. set , ( ).
exclude
, :
blocks = [(5, 7), (2, 4), (6, 10)]
:
exclude = {2, 3, 5, 6, 7, 8, 9}
exclude = set()
for block in blocks:
exclude.update(range(*block))
. , , , , , . , , , .
def delete_blocks(iterable, blocks):
exclude = set()
for block in blocks:
exclude.update(range(*block))
return [cell for i, cell in enumerate(iterable) if i not in exclude]
test_string = '0123456789abc'
blocks = [(5, 7), (2, 4), (6, 10)]
result = ''.join(delete_blocks(test_string, blocks))
print('Before: {!r}'.format(test_string))
print('Blocks:', blocks)
print('After: {!r}'.format(result))
: delete_substring_blocks
, delete_substring_blocks, delete_blocks:
def delete_substring_blocks(s, blocks):
return ''.join(delete_blocks(s, blocks))