finditer
Jason Friedman
jsf80238 at gmail.com
Mon Jul 7 23:38:27 EDT 2014
On Mon, Jul 7, 2014 at 1:19 AM, gintare <g.statkute at gmail.com> 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)
More information about the Python-list
mailing list