Answers:
words = [w.replace('[br]', '<br />') for w in words]
这些称为列表推导。
['word STRING', 'word_count BIGINT', 'corpus STRING', 'corpus_date BIGINT']
尝试将其替换'
为空,但这不起作用。我们如何用这个替换它?
如果您想知道不同方法的性能,请参考以下时间安排:
In [1]: words = [str(i) for i in range(10000)]
In [2]: %timeit replaced = [w.replace('1', '<1>') for w in words]
100 loops, best of 3: 2.98 ms per loop
In [3]: %timeit replaced = map(lambda x: str.replace(x, '1', '<1>'), words)
100 loops, best of 3: 5.09 ms per loop
In [4]: %timeit replaced = map(lambda x: x.replace('1', '<1>'), words)
100 loops, best of 3: 4.39 ms per loop
In [5]: import re
In [6]: r = re.compile('1')
In [7]: %timeit replaced = [r.sub('<1>', w) for w in words]
100 loops, best of 3: 6.15 ms per loop
如您所见,对于这种简单的模式,可接受的列表理解是最快的,但请查看以下内容:
In [8]: %timeit replaced = [w.replace('1', '<1>').replace('324', '<324>').replace('567', '<567>') for w in words]
100 loops, best of 3: 8.25 ms per loop
In [9]: r = re.compile('(1|324|567)')
In [10]: %timeit replaced = [r.sub('<\1>', w) for w in words]
100 loops, best of 3: 7.87 ms per loop
这表明对于更复杂的替换,预编译的reg-exp(如中的9-10
)可以更快。这实际上取决于您的问题和reg-exp的最短部分。
resname = [name.replace('DA', 'ADE').replace('DC', 'CYT').replace('DG', 'GUA').replace('DT', 'THY') for name in ncp.resname()]