简体   繁体   English

Python:替换列表中特定单词的所有出现的快速方法?

[英]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.

相关问题 python regex替换所有以“:”开头的单词的出现,下一个字符为字母 - python regex replace all occurances of word that starts with “:” and next character is letter 用列表中的不同值替换一个字符串的所有出现-Python - Replace all occurances of one string with different values in a list - Python python regex用匹配的字符串替换所有出现的事件 - python regex replace all occurances with the matched string 替换矩阵 python 中所有出现的数字 - Replace all occurances of a number in a matrix python 如何使用python regex查找单引号不在单词内的所有出现 - How to find all occurances of a single quote not within a word with python regex 在 Python 中返回没有特定元素的列表的快速方法 - A quick way to return list without a specific element in Python 将类中所有实例变量转换为列表的快速方法(Python) - Quick way to convert all instance variables in a class, to a list (Python) 计算 python 中列表列表列中特定值的出现次数 - Count occurances of a specific value in columns of list of list in python 在Python中拒绝列表的快速方法 - Quick way to reject a list in Python 遍历 python 中的字典列表以查找一对值的所有出现 - Iterating over a list of dictionaries in python to find all occurances of a pair of values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM