如果字符串列在

时间:2017-10-23 21:22:10

标签: python string

我有以下字符串:

s = 'in may 1999, nothing really happened, same as in june 1999'

month_set = {'may', 'june', 'july'}

我想将month_set列表中与单词匹配的所有单词替换为单词month。输出应该看起来像

  

'在1999年,没有发生任何事情,与1999年相同'

for month in month_set:
    sentence = s.replace(month, 'date')
    print(sentence)

但是返回了以下内容:

in may 1999, nothing really happened, same as in june 1999
in date 1999, nothing really happened, same as in june 1999
in may 1999, nothing really happened, same as in date 1999

除此之外,如果上述工作正常,我需要将它应用到一个很大的字符串列表中,这会使它变慢。我想。

3 个答案:

答案 0 :(得分:1)

你可以试试这个:

s = 'in may 1999, nothing really happened, same as in june 1999'
month_set = {'may', 'june', 'july'}
final_string = ' '.join("month" if i in month_set else i for i in s.split())

输出:

'in month 1999, nothing really happened, same as in month 1999'

纯正的正则表达式解决方案:

import re
s = 'in may 1999, nothing really happened, same as in june 1999'
month_set = {'may', 'june', 'july'}
final_string = re.sub('|'.join("(?<=\s){}(?=\s)".format(i) for i in month_set), 'month', s)
print(final_string)
s1 = 'may june mayor'
final_string1 = re.sub('|'.join("((?<=\s)|(?<=^)){}((?=\s)|(?=$))".format(i) for i in month_set), 'month', s1)
print(final_string1)

输出:

'in month 1999, nothing really happened, same as in month 1999'
'month month mayor'

答案 1 :(得分:1)

您的问题是s在循环中是本地的。在每次迭代时,会为其分配s的值,并替换该迭代。尝试将其替换为for month in month_set: s = s.replace(month, 'date') print(s) ,以便每次迭代的更改实际上都会持续存在。即:

{{1}}

答案 2 :(得分:1)

使用正则表达式:

import re
months = ['May', 'June']
myRegex = re.compile("|".join(months))
myRegex.sub("Month", "Welcome to May, next is June")

此版本区分大小写,但如果您需要不区分大小写的版本,请使用:

myRegex = re.compile("|".join(months), re.IGNORECASE)