Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to remove specific word from an array that is starts with "[ "?

I have a array that contains many sentences. I have split this sentences into words and make another array. I want that the words that id start with "[" and end with "]" are removed from my array.

ex.

from nltk import sent_tokenize
sentences = sent_tokenize(text)
print(sentences[0])
z= np.array(sentences)

sentence: [42] On 20 January 1987, he also turned out as substitute for Imran Khan’s side in an exhibition game at Brabourne Stadium in Bombay, to mark the golden jubilee of Cricket Club of India.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

words = z[0].split()
words= list(words)
print(words)

after split into words : [‘[42]’, ‘On’, ’20’, ‘January’, ‘1987,’, ‘he’, ‘also’, ‘turned’, ‘out’, ‘as’, ‘substitute’, ‘for’, ‘Imran’, "Khan’s", ‘side’, ‘in’, ‘an’, ‘exhibition’, ‘game’, ‘at’, ‘Brabourne’, ‘Stadium’, ‘in’, ‘Bombay,’, ‘to’, ‘mark’, ‘the’, ‘golden’, ‘jubilee’, ‘of’, ‘Cricket’, ‘Club’, ‘of’, ‘India.’]

Now I want to remove [42] from my array. and then join this words into sentence. How can I do that?
I tried this way. but this is not working. it remove whole array and print None.

for i in words:
  if i[0]=="[":
    b=words.remove(i)
    print(b)
  else:
    print("")

>Solution :

You may consider using list comprehension as below:

sentence = "[42] On 20 January 1987, he also turned out as substitute for Imran Khan's side in an exhibition game at Brabourne Stadium in Bombay, to mark the golden jubilee of Cricket Club of India."
words = sentence.split()
words = [ w for w in words if w[0]!='[' and w[-1]!= ']' ]
filtered = ' '.join(words)
print(filtered)
"On 20 January 1987, he also turned out as substitute for Imran Khan's side in an exhibition game at Brabourne Stadium in Bombay, to mark the golden jubilee of Cricket Club of India."
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading