Python: Find X to Y in a list of strings.

Posted by TheLizardKing on Stack Overflow See other posts from Stack Overflow or by TheLizardKing
Published on 2010-06-02T16:32:22Z Indexed on 2010/06/02 16:33 UTC
Read the original article Hit count: 249

Filed under:

I have a list of maybe a 100 or so elements that is actually an email with each line as an element. The list is slightly variable because lines that have a \n in them are put in a separate element so I can't simply slice using fixed values. I essentially need a variable start and stop phrase (needs to be a partial search as well because one of my start phrases might actually be Total Cost: $13.43 so I would just use Total Cost:.) Same thing with the end phrase. I also do not wish to include the start/stop phrases in the returned list. In summary:

email = ['apples','bananas','cats','dogs','elephants','fish','gee'] start = 'ban' stop = 'ele'

the magic here

new_email = ['cats','dogs']

NOTES

  • While not perfect formatting of the email, it is fairly consistent so there is a slim chance a start/stop phrase will occur more than once.
  • There are also no blank elements.

© Stack Overflow or respective owner

Related posts about python