Iterator Navigation

I would like to use regular expressions to extract information from some chat logs. The format of the lines that are processed is 03:22:32 PM <b>blcArmadillo</b>. I used the python type () command to find that the variable messages are a callable-iterator. My question is: how can I navigate the called iterator most efficiently? They look like arrays where you can just use an index? The only way I could find the “retrieval” of the data is to iterate over the return values ​​and add them to the list, as shown below in the snipet code.

times = []
messages = re.compile(r'(?P<time>..:..:.. ..).*?<b>(?P<usrname>.*?):</b>').finditer(search)

for result in messages:
    times.append(result.group('time'))

Is there a more efficient way to do this? Thanks for the help.

+3
source share
1 answer

- . , , . , . :

for result in messages:
    times.append(result.group('time'))

, :

times = [result.group('time') for result in messages]

. , , . , , . , , , , , .

: , , , , .

+4

Source: https://habr.com/ru/post/1707171/


All Articles