Python反向迭代分割单词

标签 python python-3.x

我需要将我的单词和标点符号分开- 我正在考虑让该函数查看每个单词并确定其中是否有标点符号,方法是从 [-1] 开始,索引为 -1,然后在单词遇到字母时立即将其与标点符号分开而不是标点符号...

sent=['I', 'went', 'to', 'the', 'best', 'movie!','Do','you', 'want', 'to', 'see', 'it', 'again!?!']
import string
def revF(List):
    for word in List:
        for ch in word[::-1]:
            if ch is string.punctuation:
                 #go to next character and check if it's punctuation
                newList= #then split the word between the last letter and first puctuation mark
    return newList

最佳答案

如果您只想从字符串中删除标点符号。 Python 提供了更好的方法来做到这一点 -

>>> import string
>>> line
'I went to the best movie! Do you want to see it again!?!'
>>> line.translate(None, string.punctuation)
'I went to the best movie Do you want to see it again'

关于Python反向迭代分割单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17714141/

相关文章:

python - Tensorflow 未安装,软件包与哈希文件不匹配

python - 了解 python 中的文件位置 - 意外错误

python - Flask/Django 在 View 函数之外维护状态数据

Python字典键(类对象)与多个比较器的比较

python - 如何在字符串匹配中使用通配符

python-3.x - DistutilsError : Could not find suitable distribution for Requirement. 解析 ('setuptools-scm' )

python - 我可以使用 SocksiPy 在函数内更改 SOCKS 代理吗?

python - PDF miner - 提取字体大小?

python - 如何为 Python 3 安装 `cv`?

python - 我将如何迭代 Pandas 系列并将其与单个 float 进行比较?