选择列表中包含的子字符串(无语法字符)

时间:2019-05-24 01:00:51

标签: python

我正在尝试查找和格式化列表中包含的单词。

以下是我尝试过的内容:

word_list = ['Penny', 'cat', 'carnival']

example_string= "I took my dog Penny and my cat, to the carnival."

formatted_text = " ".join(["<b>{}</b>".format(word) if word.strip(' , . ; :;\ " ()[]{}') in word_list else word for (i, word) in enumerate(example_string.split(" "))])

输出: 我把我的狗 Penny 和我的带到了狂欢节。

所需的输出: 我把我的狗 Penny 和我的带到了狂欢节

基本上,我想对字符串中的每个单词进行迭代,如果单词在列表中,则对其进行格式化,而不格式化句点,逗号,引号等标点符号。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:2)

TRY:-

word_list = ['Penny', 'cat', 'carnival']

example_string = "I took my dog Penny and my cat, to the carnival."

for x in word_list:
    example_string = example_string.replace(x, "<b>" + x + "</b>")

print(example_string)

OUTPUT:-

I took my dog <b>Penny</b> and my <b>cat</b>, to the <b>carnival</b>.

WHEN VIEWED ON BROWSER:-

I took my dog Penny and my cat, to the carnival.

答案 1 :(得分:2)

您可以使用与word_list中出现的单词的任何实例匹配的正则表达式,并用封闭的粗体标签替换它:

import re

regex = re.compile(f"({'|'.join(word_list)})")

result = re.sub(regex, r'<b>\1</b>', example_string)

print(result)

输出:

I took my dog <b>Penny</b> and my <b>cat</b>, to the <b>carnival</b>.