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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -