【问题标题】:Find out all the punctuations in a sentence that has been put into a list找出已放入列表的句子中的所有标点符号
【发布时间】:2016-10-08 16:26:24
【问题描述】:

我有这个变量:

Words = ["Hi",".","how","are","you","?","I","feel","like","I","could",",","do","better"]

从这里我想要一个变量,它可以找到所有标点符号并将其放入一个列表中。像这样:

Punctuations = [".","?",","]

【问题讨论】:

  • 方法有很多,有没有尝试过?
  • 我使用了string.punctuation方法

标签: python list punctuation


【解决方案1】:

您可以使用string.punctuation来识别标点符号:

from string import punctuation
punctuations = [w for w in words if w in punctuation]

【讨论】:

    【解决方案2】:

    使用re.findall函数的解决方案:

    import re
    
    Words = ["Hi",".","how","are","you","?","I","feel","like","I","could",",","do","better"]
    Punctuations = re.findall("[^\w\s]+", ''.join(Words))
    
    print(Punctuations)  # ['.', '?', ',']
    

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2017-11-28
      相关资源
      最近更新 更多