Replace a substring in a list of strings

I am trying to clear my sentences and that I want to remove these tags in my sentences (they are presented as underscores, followed by the word "_UH"). Basically I want to delete a line followed by an underscore (also removing the underscore itself)

text

['hanks_NNS sir_VBP',
'Oh_UH thanks_NNS to_TO remember_VB']

Requires conclusion:

['hanks sir',
'Oh thanks to remember']

Below is the code I tried:

for i in text:
    k= i.split(" ")
    print (k)
    for z in k:
        if "_" in z:
            j=z.replace("_",'')
            print (j)

Current output:

ThanksNNS
sirVBP
OhUH
thanksNNS
toTO
rememberVB
RemindVB
+4
source share
1 answer

With regex:

You can do this with re.sub(). Match the required substring in the string and replace the substring with an empty string:

import re

text = ['hanks_NNS sir_VBP', 'Oh_UH thanks_NNS to_TO remember_VB']
curated_text = [re.sub(r'_\S*', r'', a) for a in text]
print curated_text

Conclusion:

['hanks sir', 'Oh thanks to remember']

Regex:

_\S* - Underscore followed by 0 or more non space characters

Without regex:

text = ['hanks_NNS sir_VBP', 'Oh_UH thanks_NNS to_TO remember_VB']
curated_text = [] # Outer container for holding strings in text.

for i in text:
    d = [] # Inner container for holding different parts of same string.
    for b in i.split():
        c = b.split('_')[0] # Discard second element after split
        d.append(c)         # Append first element to inner container.
    curated_text.append(' '.join(d)) # Join the elements of inner container.
    #Append the curated string to the outer container.

print curated_text

Conclusion:

['hanks sir', 'Oh thanks to remember']

Problem with your code:

'_' , '_' .

for i in text:
    k= i.split(" ")
    print (k)
    for z in k:
        if "_" in z:
            j=z.replace("_",'') # <--- 'hanks_NNS' becomes 'hanksNNS'
            print (j)
+3

Source: https://habr.com/ru/post/1668850/


All Articles