问题:查找和替换列表中的字符串值
我得到了这个清单:
words = ['how', 'much', 'is[br]', 'the', 'fish[br]', 'no', 'really']
我想[br]
用一些与之相似的奇异值代替,<br />
从而得到一个新的清单:
words = ['how', 'much', 'is<br />', 'the', 'fish<br />', 'no', 'really']
I got this list:
words = ['how', 'much', 'is[br]', 'the', 'fish[br]', 'no', 'really']
What I would like is to replace [br]
with some fantastic value similar to <br />
and thus getting a new list:
words = ['how', 'much', 'is<br />', 'the', 'fish<br />', 'no', 'really']
回答 0
words = [w.replace('[br]', '<br />') for w in words]
这些称为列表推导。
回答 1
您可以使用,例如:
words = [word.replace('[br]','<br />') for word in words]
You can use, for example:
words = [word.replace('[br]','<br />') for word in words]
回答 2
除了列表理解之外,您还可以尝试地图
>>> map(lambda x: str.replace(x, "[br]", "<br/>"), words)
['how', 'much', 'is<br/>', 'the', 'fish<br/>', 'no', 'really']
Beside list comprehension, you can try map
>>> map(lambda x: str.replace(x, "[br]", "<br/>"), words)
['how', 'much', 'is<br/>', 'the', 'fish<br/>', 'no', 'really']
回答 3
如果您想知道不同方法的性能,请参考以下时间安排:
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的最短部分。
In case you’re wondering about the performance of the different approaches, here are some timings:
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
as you can see for such simple patterns the accepted list comprehension is the fastest, but look at the following:
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
This shows that for more complicated substitutions a pre-compiled reg-exp (as in 9-10
) can be (much) faster. It really depends on your problem and the shortest part of the reg-exp.
回答 4
一个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']
An example with for loop (I prefer List Comprehensions).
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']