0

sentence = 'Two Dogs and Three Cats'

list_of_indices = [[0,3], [13, 18]]

I wish to remove multiple indices from a string based on its index position by referencing a nested list of index values (which may have more than 2 elements).

I am able to remove a single index range from the string (e.g remove the word Two from the sentence using S = sentence[:0] + sentence[3:] but how do I modify this code to remove multiple range of indices by referencing list_of_indices?

I tried one other method:

to_remove = []

for i in list_of_indices:
    word = sentence[i[0]: i[1]]
    to_remove.append(word)

resultwords  = [word for word in sentence.split() if word not in to_remove]
print(resultwords)
result = ' '.join(resultwords)
print(result)

However, it would not work if the word contains other symbols in the sentence e.g. for sentence = ':Two: Dogs and Three Cats', it would not remove Two

5
  • You keep using "words" and "letters" interchangeably. Which are you trying to remove? Letters, whole words, or both? Commented Aug 25, 2021 at 4:55
  • @blorgon I am trying to remove based on index position, the values are stored in list_of_indices Commented Aug 25, 2021 at 4:56
  • question: will list_of_indices be a sorted list with no overlapping indices? Commented Aug 25, 2021 at 4:57
  • @theusaf It is sorted with no overlap Commented Aug 25, 2021 at 4:58
  • @abc that doesn't answer my question. "Remove by index position" implies removing single characters, but your code seems to be trying to remove whole words via slicing. Why are you doing it this way? Where are your lists of slice indices coming from? What does your statement at the end claiming "it would not work" mean? If your slice indices are correct then it would still work. Commented Aug 25, 2021 at 5:05

3 Answers 3

2

The key, I think, is not to think about removing, but to think about what you're keeping.

sentence = 'Two Dogs and Three Cats'
list_of_indices = [[0,3], [13, 18]]

keep = []
last = 0
for a,b in list_of_indices:
    keep.append( sentence[last:a] )
    last = b
keep.append(sentence[last:])
print( ''.join(keep) )

Output:

 Dogs and Cats
Sign up to request clarification or add additional context in comments.

Comments

1

If complexity isn't a concern, here is an O(NM) approach with the method you suggested, where N is the length of sentence, M is the length of list_of_indices. But be aware of the extraneous space introduced between words that you might want to take care of.

sentence = 'Two Dogs and Three Cats'

list_of_indices = [[0,3], [13, 18]]


for indices in reversed(list_of_indices):
    sentence = sentence[:indices[0]] + sentence[indices[1]:]

print(sentence)

output:

Dogs and  Cats

Comments

-1

You could convert it to a list then add the letters based on index to a new string:

word = 'Hello'
indices = [0, 4]

word_list = list(word)
new_word = ''
for index, value in enumerate(word_list):
    if not index in indices:
        new_word += value

print(new_word)

Output:

ell

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.