Doctest字母提取

发布于 2025-01-17 06:51:40 字数 887 浏览 0 评论 0原文

提取文本字符串中存在的所有唯一字母。 :param text: 字符串数据 :return: 字符串中所有唯一字母的小写元组

 def extract_letters(text):
    """
        Extract all the unique letters present in the text string.
        :param text: string data
        :return: a tuple of all the unique letters of the string in lowercase
        >>> extract_letters("Python 123 is C00!_")
        ('p', 'y', 't', 'h', 'o', 'n', 'i', 's', 'c')
        >>> extract_letters("Numbers Are Overrated!")
        ('n', 'u', 'm', 'b', 'e', 'r', 's', 'a', 'o', 'v', 't', 'd')
    """
    letters = []
    users = []
    
    for i in range(len(text)):
        if (text[i]).isalpha():
            letters.append(text[i].lower())
    
    for i in range(len(letters)):
        if letters[i] not in users:
            users.append(letters[i])
    
    return (users)

想知道是否有一种方法可以使输出用尖括号代替方括号,以便匹配并完成文档测试?

Extract all the unique letters present in the text string.
:param text: string data
:return: a tuple of all the unique letters of the string in lowercase

 def extract_letters(text):
    """
        Extract all the unique letters present in the text string.
        :param text: string data
        :return: a tuple of all the unique letters of the string in lowercase
        >>> extract_letters("Python 123 is C00!_")
        ('p', 'y', 't', 'h', 'o', 'n', 'i', 's', 'c')
        >>> extract_letters("Numbers Are Overrated!")
        ('n', 'u', 'm', 'b', 'e', 'r', 's', 'a', 'o', 'v', 't', 'd')
    """
    letters = []
    users = []
    
    for i in range(len(text)):
        if (text[i]).isalpha():
            letters.append(text[i].lower())
    
    for i in range(len(letters)):
        if letters[i] not in users:
            users.append(letters[i])
    
    return (users)

Was wondering if there was a way to make the output curved brackets instead of square brackets so it matches and the doctest completes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤者何惧 2025-01-24 06:51:41

试试这个:

def extract_letters(text):
    """
        Extract all the unique letters present in the text string.
        :param text: string data
        :return: a tuple of all the unique letters of the string in lowercase
        >>> extract_letters("Python 123 is C00!_")
        ('p', 'y', 't', 'h', 'o', 'n', 'i', 's', 'c')
        >>> extract_letters("Numbers Are Overrated!")
        ('n', 'u', 'm', 'b', 'e', 'r', 's', 'a', 'o', 'v', 't', 'd')
    """
    letters = []
    users = []
    
    for i in range(len(text)):
        if (text[i]).isalpha():
            letters.append(text[i].lower())
    
    for i in range(len(letters)):
        if letters[i] not in users:
            users.append(letters[i])
    
    return tuple((users)) #returns result converted from list to tuple.

Try this:

def extract_letters(text):
    """
        Extract all the unique letters present in the text string.
        :param text: string data
        :return: a tuple of all the unique letters of the string in lowercase
        >>> extract_letters("Python 123 is C00!_")
        ('p', 'y', 't', 'h', 'o', 'n', 'i', 's', 'c')
        >>> extract_letters("Numbers Are Overrated!")
        ('n', 'u', 'm', 'b', 'e', 'r', 's', 'a', 'o', 'v', 't', 'd')
    """
    letters = []
    users = []
    
    for i in range(len(text)):
        if (text[i]).isalpha():
            letters.append(text[i].lower())
    
    for i in range(len(letters)):
        if letters[i] not in users:
            users.append(letters[i])
    
    return tuple((users)) #returns result converted from list to tuple.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文