简体   繁体   English

有没有办法在Python中查找和删除字符串中的标点符号?

[英]Is there a way to find and remove punctuation from a string in Python?

I have created a function that returns a boolean if a argument contains every letter in the ascii.lowercase string (panagram). 我创建了一个函数,如果参数包含ascii.lowercase字符串(panagram)中的每个字母,则返回一个布尔值。

within the code, I am using a for loop to find membership of whitespace and punctuation with the string module methods string.whitespace and string.punctuation . 在代码中,我使用for循环来查找string模块方法string.whitespacestring.punctuation的空格和标点符号的成员资格。

When testing the for loop, the special characters in string.punctuation portion seems to not be matching the special characters provide from the for loop. 在测试for循环时, string.punctuation部分中的特殊字符似乎与for循环中提供的特殊字符不匹配。

Please provide the reasoning to string.punctuation not working as planned. 请提供string.punctuation不按计划工作的原因。

import string

def ispanagram(text, alphabet = string.ascii_lowercase):
    """Return boolean if argument is contains every letter in the ascii alphabet"""

    alphabet_list = list(alphabet)    
    letter_set = sorted(set(text.lower()))

    for char in letter_set:
        if char in string.whitespace or char in string.punctuation:
            letter_set.remove(char)

    return letter_set == alphabet_list


ispanagram("The quick brown !fox jumps over the lazy dog")

The main issue is that you're modifying letter_set while iterating over it. 主要问题是你在迭代时修改了letter_set This does not work as expected ( explanation ). 这不能按预期工作( 解释 )。

To fix, iterate over a copy: 要修复,请迭代副本:

for char in letter_set[:]:

Let me know if this help. 如果有这个帮助,请告诉我。

import string
import re

def ispanagram(text, alphabet = string.ascii_lowercase):
    """Return boolean if argument is contains every letter in the ascii alphabet"""

    alphabet_list = list(alphabet)

    # just remove all the special characters including space
    text_only_chars = re.sub(r"[-()\"#/@;:<>{}`+=~|.!?, ]", "", text)

    letter_set = sorted(set(text_only_chars.lower()))

    return letter_set == alphabet_list


print(ispanagram("The quick brown !fox jumps over the lazy dog"))

#### Output ####
True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM