【问题标题】:Taking punctuation out of a a python list从 python 列表中取出标点符号
【发布时间】:2012-06-07 16:13:33
【问题描述】:

我知道有很多关于删除标点符号的例子,但我想知道最有效的方法。我有一个从 txt 文件中读取并拆分的单词列表

wordlist = open('Tyger.txt', 'r').read().split()

检查每个单词并删除任何标点符号的最快方法是什么?我可以用一堆代码做到这一点,但我知道这不是最简单的方法。

谢谢!!

【问题讨论】:

  • 您能否提供一个示例输入和输出(或描述组成您的标点符号集的内容)?
  • 确定没问题。文本文件是一首诗。前两行写着:Tyger!泰格!在夜晚的森林中燃烧起来,我希望它们以没有逗号或感叹号的形式出现在列表中。我需要删除的一组标点符号是“-,!?。谢谢!
  • @JoranBeasley:我不认为这是一个骗局。我的回答适合这个问题,但不适合另一个。

标签: python text performance punctuation


【解决方案1】:

我认为最简单的方法是首先只提取由字母组成的单词:

import re

with open("Tyger.txt") as f:
    words = re.findall("\w+", f.read())

【讨论】:

  • 这将如何处理不是标点符号的特殊字符?
  • 非常感谢。我真的很感激帮助。我从你们所有人那里学到了很多
  • @EnglishGrad:注意 Sven 使用 with 关键字打开输入文件。使用with 块优于使用f = open()... close()much 优于使用stuff = open().read()...。在最后一个示例中,您无法在读/写后显式地close() 文件。
  • @luke14free:通过提供re.LOCALEre.UNICODE 标志并设置语言环境,您可以根据需要执行此操作。对于没有任何标志的标准字符串,它只会匹配集合[a-zA-Z0-9_]。有关详细信息,请参阅documentation
【解决方案2】:

我会选择这样的:

import re
with open("Tyger.txt") as f:
    print " ".join(re.split("[\-\,\!\?\.]", f.read())

它将仅删除真正需要的内容,并且不会由于过度匹配而造成过度过载。

【讨论】:

    【解决方案3】:

    例如:

    text = """
    Tyger! Tyger! burning bright
    In the forests of the night,
    What immortal hand or eye
    Could frame thy fearful symmetry? 
    """
    import re
    words = re.findall(r'\w+', text)
    

    import string
    ps = string.punctuation
    words = text.translate(string.maketrans(ps, ' ' * len(ps))).split()
    

    第二个更快。

    【讨论】:

    • 请注意,您的两个解决方案做不同的事情。 "fearful,symmetry" 在第二种方法中最终会变成一个单词。
    • @SvenMarnach:是的,正确的。尽管如此,翻译的速度还是比 re 快 4 倍。
    【解决方案4】:
    >>> import re
    
    >>> the_tyger
    '\n    Tyger! Tyger! burning bright \n    In the forests of the night, \n    What immortal hand or eye \n    Could frame thy fearful symmetry? \n    \n    In what distant deeps or skies \n    Burnt the fire of thine eyes? \n    On what wings dare he aspire? \n    What the hand dare sieze the fire? \n    \n    And what shoulder, & what art. \n    Could twist the sinews of thy heart? \n    And when thy heart began to beat, \n    What dread hand? & what dread feet? \n    \n    What the hammer? what the chain? \n    In what furnace was thy brain? \n    What the anvil? what dread grasp \n    Dare its deadly terrors clasp? \n    \n    When the stars threw down their spears, \n    And watered heaven with their tears, \n    Did he smile his work to see? \n    Did he who made the Lamb make thee? \n    \n    Tyger! Tyger! burning bright \n    In the forests of the night, \n    What immortal hand or eye \n    Dare frame thy fearful symmetry? \n    '
    
    >>> print re.sub(r'["-,!?.]','',the_tyger)
    

    打印:

    Tyger Tyger burning bright 
    In the forests of the night 
    What immortal hand or eye 
    Could frame thy fearful symmetry 
    
    In what distant deeps or skies 
    Burnt the fire of thine eyes 
    On what wings dare he aspire 
    What the hand dare sieze the fire 
    
    And what shoulder  what art 
    Could twist the sinews of thy heart 
    And when thy heart began to beat 
    What dread hand  what dread feet 
    
    What the hammer what the chain 
    In what furnace was thy brain 
    What the anvil what dread grasp 
    Dare its deadly terrors clasp 
    
    When the stars threw down their spears 
    And watered heaven with their tears 
    Did he smile his work to see 
    Did he who made the Lamb make thee 
    
    Tyger Tyger burning bright 
    In the forests of the night 
    What immortal hand or eye 
    Dare frame thy fearful symmetry 
    

    或者,使用文件:

    >>> with open('tyger.txt', 'r') as WmBlake:
    ...    print re.sub(r'["-,!?.]','',WmBlake.read())
    

    如果你想创建一个行列表:

    >>> lines=[]
    >>> with open('tyger.txt', 'r') as WmBlake:
    ...    lines.append(re.sub(r'["-,!?.]','',WmBlake.read()))
    

    【讨论】:

    • +1 用于发布完整的诗歌;)虽然它现在看起来更像是布考斯基而不是布莱克。
    猜你喜欢
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 2015-09-07
    • 2013-10-25
    • 1970-01-01
    • 2014-01-01
    相关资源
    最近更新 更多