Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news2.arglkargh.de!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.052 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'patterns': 0.04; '"""': 0.07; 'cc:addr:python-list': 0.11; 'def': 0.12; 'itertools': 0.16; 'wrote:': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'task': 0.26; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'consisting': 0.31; 'text': 0.33; 'url:python': 0.33; 'maybe': 0.34; 'could': 0.34; 'advice': 0.35; 'received:google.com': 0.35; 'url:org': 0.36; 'url:library': 0.38; 'how': 0.40; 'break': 0.61; 'url:3': 0.61; 'times': 0.62; 'to:addr:gmail.com': 0.65; 'skip:r 30': 0.69; 'jul': 0.74; 'x):': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=FP/W0Go3RiggweB5y/f+mSegjh0uFTbpY/20LtIXuU4=; b=m0hno9Hh3uoNmYa04kEhsyyxBXpgb4uFi0Ci5Aoqq+hVQ0RmXcmLS8tewibxoy39TT /QfKZvt9JFfxZYnkhi5jg94XcHtR+Un+wuXj5mESRCw/G4dyfpa3v9RsmuK0FYIPlzxM ScFqyXj+XamT8+5npFgIZQvIw+F/fFWDE99DaYDip/6dvIXe/LD5y8wfs/pjrAJiP0iL K2lAwTOT6VtmwkKXsICdBveKJZ43dVEg6Xmwuovgu/Z6uRizon++vXZhG6dbNUb8bivF 6fOgg3tVjRjGrgPYg8zk3C9XMx9D8bCtp+UcIaIMZZXzD45EdlXFWyKDcfek3ZpzOw+K 7VWg== MIME-Version: 1.0 X-Received: by 10.224.134.201 with SMTP id k9mr55282429qat.59.1404790707892; Mon, 07 Jul 2014 20:38:27 -0700 (PDT) In-Reply-To: References: Date: Mon, 7 Jul 2014 21:38:27 -0600 Subject: Re: finditer From: Jason Friedman To: gintare Content-Type: text/plain; charset=UTF-8 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1404790710 news.xs4all.nl 2880 [2001:888:2000:d::a6]:60931 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74147 On Mon, Jul 7, 2014 at 1:19 AM, gintare wrote: > If smbd has time, maybe you could advice how to accomplish this task in faster way. > > I have a text = """ word{vb} > wordtransl {vb} > > sent1. > > sent1trans. > > sent2 > > sent2trans... """ > > I need to match once wordtransl, and than many times repeating patterns consisting of sent and senttrans. You might try itertools.groupby (https://docs.python.org/3/library/itertools.html#module-itertools). text = """ word{vb} wordtransl {vb} sent1 sent1trans sent2 sent2trans """ import itertools import re result_list = list() lines = text.split("\n") for line in lines[:]: if line.startswith("sent"): break lines.pop(0) def is_start(x): pattern = re.compile(r"sent\d+$") if re.search(pattern, x): return True for key, mygroup in itertools.groupby(lines, is_start): result_list.append(list(mygroup)) print(result_list)