Possible Duplicate:
How to find positions of the list maximum?
A question from homework:
Define a function censor(words,nasty) that takes a list of words, and
replaces all the words appearing in nasty with the word CENSORED, and
returns the censored list of words.
>>> censor([’it’,’is’,’raining’], [’raining’])
[’it’,’is’,’CENSORED’]
I see solution like this:
- find an index of
nasty - replace words matching that index
with"CENSORED"
but i get stuck on finding the index..
Actually you don’t have to operate with indexes here. Just iterate over
wordslist and check if the word is listed innasty. If it is append'CENSORED'to the result list, else append the word itself.Or you can involve list comprehension and conditional expression to get more elegant version: