python-3.x - 不使用正则表达式拆分标点符号
问题描述
给定输入:
Democr _acy , is overrat _ed .
期望的输出:
Democracy, is overrated.
这是我的代码:
sentence=input()
punctuation = "!\"#$%&'()*+,-./:;<=>?@[\]^`{|}~"
suffixes = ["acy", "ance", "ence", "dom", "er", "or", "ism", "ist",
"ty", "ment", "ness", "ship", "sion", "tion", "ate",
"en", "fy", "ize", "able", "ible", "al",
"esque", "ful", "ic", "ous", "ish", "ive",
"less", "ed", "ing", "ly", "ward", "wise"]
sentence_list = sentence.split('_')
c=""
if c not in punctuation:
print("".join(sentence_list))
elif c in punctuation:
for c in sentence:
print("".join(sentence_list).split(c))
如您所见,我的输出有 29 个不同的列表,但我只想要其中一个。我想从单词中删除 ' ' 并加入我从中删除 ' ' 的标点符号和单词。当我编写如下代码时:
sentence_list = sentence.split('_')
print("".join(sentence_list))
'_' 和标点符号消失。我在哪里做错了?
解决方案
这就是我要解决这个问题的方法。
def combineSentence(si):
punctuation = "!\"#$%&'()*+,-./:;<=>?@[\]^`{|}~"
rslt = ''
ptr = 0
while ptr < len(si):
if si[ptr] not in punctuation and si[ptr] != '_':
rslt += si[ptr]
else:
rslt = rslt[:-1]
if si[ptr] in punctuation:
rslt += si[ptr]
ptr += 1
return rslt
执行combineSentence('Democr _acy , is overrat _ed .')
将产生:
'Democracy, is overrated.'
推荐阅读
- c# - 通过网络或 Internet 将数据值保存到 SQL Server 数据库
- java - 在 Java 中直接访问 ArrayList 中的列
- python - scipy包的pyinstaller错误
- elasticsearch - Elastic Search 即时更新映射
- windows - Windows cmd git bash:conda.sh 没有这样的文件或目录(Windows 行结尾,缺少斜杠)
- common-lisp - 动态绑定变量:恢复到以前的状态
- html - MS Edge 中出现和消失的 CSS 边框
- java - 用于生成给定 JSON 字符串的 Java 类
- c++ - getline() 取行结束和下一行开始
- java - 在 Android 上使用椭圆曲线密钥对