gpt4 book ai didi

python - Python 2 中的字符串替换和匹配

转载 作者:太空宇宙 更新时间:2023-11-04 03:56:31 25 4
gpt4 key购买 nike

我有一些用户帖子,我想将其与预定的模式列表相匹配(参见示例)。如果帖子与模式匹配,我想将帖子和模式写入文件。做这个的最好方式是什么?到目前为止,我只想到用 4 个 for 循环强制执行它,然后进行一些比较。我已经有了我需要的所有数据的列表,下面只是一些非常简单的示例,可以让您了解我在寻找什么。


示例

帖子:

posts =['When I ate at McDonald\'s, I felt sick.',
'I like eating at Burger King.',
'Wendy\'s made me feel happy.']

图案:

patterns = ['When I ate at [RESTAURANT]',
'I like eating at [RESTAURANT]',
'[RESTAURANT] made me feel [FEELING]',
'I felt [FEELING]']

列表:

restaurant_names = ['McDonald\'s', 'Burger King', 'Wendy\'s']

feelings = ['happy', 'sick', 'tired']

输出文件:

当我在 [RESTAURANT] 吃饭时,当我在麦当劳吃饭时,我感到恶心。

我感到[FEELING],当我在麦当劳吃饭时,我感到恶心。

[RESTAURANT] 让我感到[FEELING],Wendy 让我感到快乐。

我喜欢在 [RESTAURANT] 吃饭,我喜欢在汉堡王吃饭。


- 抱歉格式化,但这是我潜伏了一段时间后在 stackoverflow 上发表的第一篇文章。在此先感谢您的帮助!

最佳答案

这样的事情怎么样:

>>> sentences = ["When I ate at McDonald's, I felt sick.", 'I like eating at Burger King.', 
"Wendy's made me feel happy."]
>>> patterns = {"McDonald's": "[RESTAURANT]", "Burger King": "[RESTAURANT]",
"Wendy's": "[RESTAURANT]", "happy": "[FEELING]", "sick": "[FEELING]",
"tired": "[FEELING]"}

然后你可以做

>>> for sentence in sentences:
... replaced = sentence
... for pattern in patterns:
... if pattern in sentence:
... replaced = replaced.replace(pattern, patterns[pattern])
... print sentence
... print replaced
...
When I ate at McDonald's, I felt sick.
When I ate at [RESTAURANT], I felt [FEELING].
I like eating at Burger King.
I like eating at [RESTAURANT].
Wendy's made me feel happy.
[RESTAURANT] made me feel [FEELING].

这仍然需要一些工作(例如,现在,单词 carsick 将变为 car[FEELING]),您可能希望避免所有重复patterns 值,方法是创建另一个您可以按索引引用的替换文本列表,但这也许足以让您入门?

关于python - Python 2 中的字符串替换和匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18135547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com