[(i.replace('www.','',1), j) for i,j in l]
from timeit import timeit
l = "l=[('www.ya.ru', set()), ('ya.ru', set()), ('xxx.x', set())]"
setup = """
import re
r = re.compile(r'^(?:www\.)?(.*)', re.I)
""" + l
setup2 = """
import re
r = re.compile(r'^www\.', re.I)
""" + l
tests = {
'for x + split': ("[(i[0].split('www.')[-1], i[1]) for i in l]", l),
'for x,y + split': ("[(i.split('www.')[-1], j) for i,j in l]", l),
'for x,y + replace': ("[(i.replace('www.','',1), j) for i,j in l]", l),
'map + replace': ("map(lambda ij:(ij[0].replace('www.','',1), ij[1]), l)", l),
'map + lambda() + replace': ("map(lambda (i,j):(i.replace('www.','',1), j), l)", l), # py2 only
'map + match': ("map(lambda x:(r.match(x[0]).group(1),x[1]),l)", setup), # py3 compatible
'for x,y + match': ("[(r.match(x).group(1),y) for x,y in l]", setup), # py3 compatible
'map + sub': ("map(lambda x:(r.sub('',x[0],1),x[1]),l)", setup2), # py3 compatible
'for x,y + sub': ("[(r.sub('',x,1),y) for x,y in l]", setup2), # py3 compatible
'map + lambda() + match': ("map(lambda (x,y):(r.match(x).group(1),y),l)", setup), # py2 only
'map + lambda() + sub': ("map(lambda (x,y):(r.sub('',x,1),y),l)", setup2), # py2 only
}
def test_suit():
for s, (t, setup) in tests.items():
try:
yield (timeit(t, setup=setup), s)
except SyntaxError:
pass
for ts in sorted(test_suit()):
print("%.4f %s" % ts)