从Python中的单词列表中替换字符串中的多个单词

时间:2017-11-15 19:05:12

标签: string python-2.7 replace

我有一个以下格式的文本文件:

$(function(){
$(".chosen-select").trigger("chosen:updated");
})

和另一个标签列表

this is some text __label__a
this is another line __label__a __label__b
this is third line __label__x
this is fourth line __label__a __label__x __label__z

每行可以包含列表中的多个标签。 用“__label__no”

替换每行中列表中标签的最佳方法是什么?

示例:

list_labels = ['__label__x','__label__y','__label__z']

文本文件和标签中有很多行,我想知道实现这一目标的最快方法是什么。

1 个答案:

答案 0 :(得分:0)

这可能不是“最快的方法”,但根据文本文件的长度,这可能有效:

list_labels = ['__label__x','__label__y','__label__z']

with open('text.txt', 'r') as f:
    fcontents = f.readlines()

fcontents = [l.strip() for l in fcontents]

def remove_duplicates(l):
    temp = []
    [temp.append(x) for x in l if x not in temp]
    return temp

for line in fcontents:
    for ll in list_labels:
        if ll in line:
            l = line.replace(ll, '__label__no')
            line = ' '.join(remove_duplicates(l.split()))

    print line

输出:

this is some text __label__a
this is another line __label__a __label__b
this is third line __label__no
this is fourth line __label__a __label__no

借用此问题How can I remove duplicate words in a string with Python?

中的unique_list功能