python - 如何使用python中的预定义词组将字符串的单词分组为不同的字符串?
问题描述
我想将包含以下单词的字符串转换为: The Red Fox The Cat The Dog Is Blue 为 3 个字符串,其中第一个包含 The Red Fox,第二个包含 The Cat,最后一个包含 The Dog Is Blue一。更简单的解释是,它应该这样做:
# String0 = The Red Fox The Cat The Dog Is Blue
# The line above should transform to the lines below
# String1 = The Red Fox
# String2 = The Cat
# String3 = The Dog Is Blue
您必须注意,构成表达式的单词是要改变的(但仍会形成已知的表达式),因此我正在考虑制作一本字典,这将有助于识别单词并定义它们应该如何组合在一起,如果可能的话。
我希望我是可以理解的,并且有人会回答我的问题。
解决方案
这将为您提供所需的基本代码:
def separate():
string0 = "The Red Fox The Cat The Dog Is Blue"
sentences = ["The "+sentence.strip() for sentence in string0.lower().split("the") if sentence != ""]
for sentence in sentences:
print(sentence)
推荐阅读
- java - 在静态服务中返回数据源的静态方法
- c# - 如何使用 AddLine 创建 X 长度的线
- html - 将页面划分为 6
- java - system.getenv() 属性不读取外部 logback.xml 文件的文件路径
- php - 如何在 PHP 中解码这些字符?
- asp.net - Asp.net:如何将值从文本框分配到数据库以及从数据库分配到标签,确切地说是您在文本框中键入它的方式
- node.js - 使用节点命令行连续执行javascript文件
- python - Azure VM 似乎会杀死长时间运行的 MySql 查询
- c# - 尝试序列化字典时,SerializationException“未标记为可序列化”
- android - 滚动视图不适用于多种布局