title: What is the fastest way to replicate sentences by replacing words according to word list?
body: <p>I have a word list and some sentences I need to replicate:</p>
  
  <pre><code>wordlist_dict = {
      'class1': ['word_a', 'word_b', 'word_c'],
      'class2': ['word_d', 'word_e'],
      'class3': ['word_f', 'word_g', 'word_h', 'word_i', 'word_a']
  }
  
  sent_list = [
      "I have a sentence with word_g",
      "And another sentence with word_d",
      "Don't forget word_b",
      "no keyword here",
      "Last sentence with word_c and word_e"
  ]
  </code></pre>
  
  <p>My expected results are:</p>
  
  <pre><code>I have a sentence with word_f
  I have a sentence with word_h
  I have a sentence with word_i
  I have a sentence with word_a
  And another sentence with word_e
  Don't forget word_a
  Don't forget word_c
  Last sentence with word_a and word_d
  Last sentence with word_a and word_e
  Last sentence with word_b and word_d
  Last sentence with word_b and word_e
  Last sentence with word_c and word_d
  </code></pre>
  
  <p>Here is my method:</p>
  
  <pre><code>import re
  
  pattern_list = []
  pattern_all = ''
  wordlist = sorted(wordlist_dict.values())
  for v in wordlist:
      pattern_list.append('({})+'.format('|'.join(v)))
      pattern_all += '|' + '|'.join(v)
  pattern_all = '({})+'.format(pattern_all[1:])
  print(pattern_list)
  # ['(word_a|word_b|word_c)+', '(word_d|word_e)+', '(word_f|word_g|word_h|word_i)+']
  print(pattern_all)
  # (word_a|word_b|word_c|word_d|word_e|word_f|word_g|word_h|word_i)+
  
  new_sent_list = []
  for sent in sent_list:
      match_list = re.findall(pattern_all, sent)
      print(match_list)
      if match_list:
          for match in match_list:
              for i in range(len(pattern_list)):
                  if re.search(pattern_list[i], sent):
                      if match in wordlist[i]:
                          match_wordlist = wordlist[i]
                          match_wordlist.remove(match)
                          for word in match_wordlist:
                              new_sent_list.append(sent.replace(match, word))
                      else:
                          continue
  </code></pre>
  
  <p>And I wonder if there is any way more efficient to do this because my word list and sentence list are much larger than in the example. Thanks in advance.</p>
  
  <p>Update: I just realized there are words that belong to more than one class and sentences that have more than one keyword, so my code doesn't work now.</p>