Slow List Comprehension with Regex

You could use a regex with .groupby:

from itertools import groupby 
import re


li=['regular0000.png', 'regular0001.png', 'regular0002.png', 
 'depth.0000.png', 'emission0000.png', 'emission0001.png',   
 'emission0002.png', 'diffusefilter0000.png']

for k,v in groupby(sorted(li), key=lambda s: re.search(r'(^[^\d]+)', s).group(1)):
    print(k,list(v))

Prints:

depth. ['depth.0000.png']
diffusefilter ['diffusefilter0000.png']
emission ['emission0000.png', 'emission0001.png', 'emission0002.png']
regular ['regular0000.png', 'regular0001.png', 'regular0002.png']

Then if you want only groups:

for k,v in groupby(sorted(li), key=lambda s: re.search(r'(^[^\d]+)', s).group(1)):
    tgt=list(v)
    if len(tgt)>1: print(tgt)

If I run this with your example *10000 it completes the entire list in 152 ms…

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top