在列表中查找和替换字符串值

206

我有一个列表:

words = ['how', 'much', 'is[br]', 'the', 'fish[br]', 'no', 'really']
我想要的是用类似于<br />的值替换[br],从而得到一个新列表。
words = ['how', 'much', 'is<br />', 'the', 'fish<br />', 'no', 'really']
6个回答

364
words = [w.replace('[br]', '<br />') for w in words]

这被称为 列表推导式


41

你可以使用以下方法:

words = [word.replace('[br]','<br />') for word in words]

39

除了列表推导式,你可以尝试使用map

>>> map(lambda x: str.replace(x, "[br]", "<br/>"), words)
['how', 'much', 'is<br/>', 'the', 'fish<br/>', 'no', 'really']

25

如果你想了解各种方法的性能表现,以下是一些时间记录:

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中所示)可以更快(很多)。实际上,这取决于您的问题和正则表达式的最短部分。


6

一个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']

我发现这个例子对于一个更复杂的情况非常有用,那个情况下我无法解决列表推导式。更具体地说,对于一个我没有时间解决列表推导式的情况,我后来有机会时能够用推导式替换代码。 - undefined

3
如果性能很重要,包括一个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 />')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接