4 ms·
Adventures in Python optimisation
- UncleEntity 8y agoSeems to me that it would be more efficient to modify BeautifulSoup's decoded xml output rather than doing a bunch of string search/replacement/copying.
- lixtra 8y agoInstead of going through each file len(note_range) times wouldn’t it be better to search for noteref-[0-9]+ once and then bump the number on each found occasion?
- lixtra 8y agoOr more concrete do: s = """ But I must explain to you how all this mistaken idea of denouncing pleasure and expound the actual teachings of the great explorer of the truth, the #endnote-1 master-builder of human happiness. No one rejects, dislikes, or avoids pleasure #endnote-10 advantage from it? But who has any right to find fault with a man that produces no resultant pleasure? """ import re def bump(match): return '#endnote-{}'.format(int(match.group(1))+1) r = re.sub(r'#endnote-(\d+)', bump, s) print(r)