【发布时间】:2019-10-20 00:47:45
【问题描述】:
大家好,
假设我在 python 中有一本字典:
dict = {'fresh air', 'entertainment system', 'ice cream', 'milk', 'dog', 'blood pressure'}
以及如下文本列表:
text_file = ['is vitamin d in milk enough', 'try to improve quality level by automatic intake of fresh air', 'turn on the tv or entertainment system based on that individual preferences', 'blood pressure monitor', 'I buy more ice cream', 'proper method to add frozen wild blueberries in ice cream']
我想在文本文件的所有出现中显示每个出现的短语属于字典(比如新鲜空气)为#fresh_air#,而对于字典中的每个单词(比如milk),输出应该显示如#milk#,即在所有出现的text_file 的开头和结尾附加特殊字符。
我想要的输出应该是以下形式(列表列表):
[[is vitamin d in #milk# enough], [try to improve quality level by automatic intake of #fresh_air#], [turn on the tv or #entertainment_system# based on the individual preferences], [#blood_pressure# monitor], [I buy more #ice_cream#], [proper method to add frozen wild blueberries in #ice_cream# with #milk#]]
是否有任何标准方法可以以省时的方式实现这一目标?
我是使用 python 进行列表和文本处理的新手,我尝试过使用列表理解,但未能达到预期的结果。非常感谢任何帮助。
【问题讨论】:
-
你有一个
set对象
标签: python python-3.x list dictionary nltk