python - How do I modify all terms following a specific term in a list of terms? -
i have list of words so:
list = ['i', 'did', 'not', 'enjoy', 'the', 'movie'] so, aim is, if word 'not' appears in list of words, of following words should concatenated 'not_' on left side. example, output list above should be:
output_list = ['i', 'did', 'not', 'not_enjoy', 'not_the', 'not_movie']
if want start appending "not" after you've seen 'not', here's algorithm might work:
seen_not = false output_list = [] item in input_list: if seen_not: output_list.append("not_" + item) else: output_list.append(item) if item == "not": seen_not = true we construct new list, adding items old list 1 one. if we've seen 'not' in old list, append modified word new list.
edit: turned code function named mod_list , tried in python interpreter:
>>> mod_list(['i', 'did', 'not', 'enjoy', 'the', 'movie']) ['i', 'did', 'not', 'not_enjoy', 'not_the', 'not_movie']
Comments
Post a Comment