iterator - Joining the previous and next sentence using python -


I am trying to get involved in a set of sentences contained in a list . I have a function that determines whether there is a sentence in cost savings. However, to keep the context of punishment, I need to keep the sentence before and after this. In the case of the side, where its either first or last sentence, I only keep the sentence and its only neighbor.

An example is best:

  ex_paragraph = ['Quick brown fox jumps on fencing.', 'Where there is another red fox', 'they together Run away. ',' They survive later. '] T1 = Lambda x: x. Starts with 'T' should have the result:  
  ['Quick brown fox jumps on the fence, where there is another red fox, they move together.']  

The result of T2 should be:

  ['Quick brown fox jumps on the fence, where there is another red fox.]]  

  def YieldContext (sent_list, cont_fun): def JoinSent (sent_list, ind): if ind == 0: returns send_list [ind] + sent_list [ind + 1] elif ind == lane (sent_list) -1: return send_list [ind-1] + sent_list [ind] else: return '' .join (sent_list) The list, count (0)) has been sent: If cont_fun (sent): join the yield (sent list, sent_num)  

what is anyone like this

Thank you,

Will you find a "cleaner" or more dragon's way to do something if-alif -f / P>

PS. I'm obviously doing this with a more complex "reference function", but this is just a simple example.

Adding an empty string at the beginning and end of the list is actually a good idea, but actually a fancy list There is no understanding or such a requirement that you can create a generator very easily:

  def yieldContext (l, func): l = [']] + l + [']] Enumerate I, s (n) for: if Func (s): produce '' .join (l [i-1: i + 2]). Strip ()  

returns:

  & gt; & Gt; & Gt; Print list (yield syntaxtext (X-paragraph, T1)) ['' Quick brown fox jumps on Barnes. Where there is another red fox, they run away together. ']> Gt; & Gt; Print list (yield syntax (x-paragraph, T2)) ['Quick brown fox jumps on top of fence where there is another red fox. What do you want to do with sentences and "context")   
  def yield syntax (L, throw): l = [']] + l + [']]  
/ html>

Comments