[英]Python: quick way to replace all occurances of a specific word in a list?
I have a list of words. 我有一个词表。 It's pretty large (len(list) ~ 70,000). 它很大(len(list)〜70,000)。 I'm currently using this code: 我目前正在使用此代码:
replacement = "bla"
for word in data:
if (word in unique_words):
word = replacement
This code take a while to perform the operation. 此代码需要一段时间才能执行操作。 Is there a quicker way to do this? 有更快的方法吗?
Use a set
for unique_words
. 为unique_words
使用一个set
。 Sets are considerably faster than lists for determining if an item is in them (see Python Sets vs Lists ). 设置比确定列表中是否包含项的列表要快得多(请参阅Python设置与列表 )。
Also, it's only a stylistic issue but I think you should drop the brackets in the if
. 另外,这只是一个样式问题,但我认为您应该在if
放括号。 It looks cleaner. 看起来比较干净。
The code you have posted doesn't actually do any replacement. 您发布的代码实际上不会进行任何替换。 Here is a snippet that does: 这是一个片段,它可以:
for key,word in enumerate(data):
if word in unique_words:
data[key] = replacement
Here's a more compact way: 这是一种更紧凑的方法:
new_list = [replacement if word in unique_words else word for word in big_list]
I think unique_words
is an odd name for the variable considering its use, perhaps it should be search_list
? 考虑到变量的使用,我认为unique_words
是一个奇怪的名字,也许应该是search_list
?
Edit : 编辑 :
After your comment, perhaps this is better: 发表评论后,也许这样更好:
from collections import Counter
c = Counter(data)
only_once = [k for k,v in c.iteritems() if v == 1]
# Now replace all occurances of these words with something else
for k, v in enumerate(data):
if v in only_once:
data[k] = replacement
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.