fork download
  1. mycookbook= [["i", "love", "tim", "tam", "and", "chocolate", "ice", "cream"], ["cooking",
  2. "fresh", "vegetables", "is", "easy"], ["fresh", "vegetables", "and", "fruits", "are", "good",
  3. "for", "health"]]
  4. mylist = ["tim tam", "chocolate ice cream", "fresh vegetables and fruits"]
  5.  
  6. for sentence in mycookbook:
  7. i = 0
  8. while i < len(sentence):
  9. for j in range(i + 1, len(sentence)+1):
  10. phrase = ' '.join(sentence[i:j])
  11. if phrase in mylist:
  12. sentence[i:j] = [phrase]
  13. break
  14. i += 1
  15.  
  16. print(mycookbook)
Success #stdin #stdout 0.02s 28480KB
stdin
Standard input is empty
stdout
[['i', 'love', 'tim tam', 'and', 'chocolate ice cream'], ['cooking', 'fresh', 'vegetables', 'is', 'easy'], ['fresh vegetables and fruits', 'are', 'good', 'for', 'health']]