我有一个列表:
words = ['how', 'much', 'is[br]', 'the', 'fish[br]', 'no', 'really']
我想要的是用类似于<br />
的值替换[br]
,从而得到一个新列表。words = ['how', 'much', 'is<br />', 'the', 'fish<br />', 'no', 'really']
你可以使用以下方法:
words = [word.replace('[br]','<br />') for word in words]
除了列表推导式,你可以尝试使用map
>>> map(lambda x: str.replace(x, "[br]", "<br/>"), words)
['how', 'much', 'is<br/>', 'the', 'fish<br/>', 'no', 'really']
如果你想了解各种方法的性能表现,以下是一些时间记录:
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
这表明对于更复杂的替换,预编译的正则表达式(如9-10
中所示)可以更快(很多)。实际上,这取决于您的问题和正则表达式的最短部分。
一个for循环的例子(我更喜欢使用列表推导式)。
a, b = '[br]', '<br />'
for i, v in enumerate(words):
if a in v:
words[i] = v.replace(a, b)
print(words)
# ['how', 'much', 'is<br/>', 'the', 'fish<br/>', 'no', 'really']
if-else
子句可以提高性能(对于一个包含100万个字符串的列表,大约提高了5%,这并不是微不足道的)。replaced = [w.replace('[br]','<br />') if '[br]' in w else w for w in words]
map()
的实现可以通过使用 operator.methodcaller()
调用 replace
来进行优化(可以提高约20%),但仍然比列表推导式慢(截至 Python 3.9)。
from operator import methodcaller
list(map(methodcaller('replace', '[br]', '<br />'), words))
for i, w in enumerate(words):
if '[br]' in w:
words[i] = w.replace('[br]', '<br />')