【问题标题】:Python - How to output strings that are in a list, that have a certain number of letters in themPython - 如何输出列表中包含一定数量字母的字符串
【发布时间】:2018-09-21 02:06:18
【问题描述】:

使用 Python 3.7,我有一个包含各种长度字符串的列表。我正在尝试使用函数只返回有两个字母的字符串——我的阈值。当我真的想要打印“a”、“ab”和“ac”时,我目前得到了“a”的单个字符串输出。我不知道我哪里出错了?我知道 len(xStr) 会计算字符串中的字母数,但我不确定如何在这里正确使用它。

这是我尝试的代码:

threshold = 2
def listOfWords(list):
    stringList = ["a", "ab", "abc", "ac", "abcd"]
    return stringList

def wordsInListsCounter():
    for elements in listOfWords(list):
        if len(elements) <= threshold:
            strLessThanThreshold = elements
            return strLessThanThreshold
        elif len(elements) == 0:
            emptyString = "There are no words in this list"
            return emptyString
        else:
            error = "There is invalid information"
            return error
print(wordsInListsCounter())

任何帮助将不胜感激!我是这里的 Python 新手...

【问题讨论】:

    标签: python-3.x list function


    【解决方案1】:

    使用列表推导:

    >>> stringList = ["a", "ab", "abc", "ac", "abcd"]
    >>> modifiedList = [x for x in stringList if len(x) <= 2]
    >>> modifiedList
    ['a', 'ab', 'ac']
    

    我已经编辑了我的答案以更好地匹配您的问题,这是我要补充的内容:

    threshold = 2
    myList = ["a", "ab", "abc", "ac", "abcd"]
    
    def wordsInListsCounter(stringList):
        elements = []
        for element in stringList:
            if len(element) <= threshold:
                elements.append(element)
        return elements
    
    elements = wordsInListsCounter(myList)
    
    if len(elements) == 0:
        print("There are no words in this list")
    
    else:
        print(elements)
    

    【讨论】:

    • 谢谢。如何将您的建议纳入我目前拥有的代码中?我想尝试使用函数来做这个字符串/字母计数器的事情......
    • 您的初始代码存在一些问题:1. listOfWords 接受一个列表参数...您不使用 2.您的 for 循环适用于列表的元素...但随后您测试如果 len(elements)==0 所以我认为您正在混合元素与完整列表,这就是我分离过滤的原因,它在 wordsInListsCounter 中,您检查列表长度的后处理在过滤完成后完成
    • 我正试图弄清楚在哪里以及如何修复它:-)
    • 是的,我希望我在最后一条评论中所说的对您有所帮助!
    【解决方案2】:

    将@kevh 的答案合并到您的代码中如下所示:

    threshold = 2
    
    def listOfWords(list):
        stringList = ["a", "ab", "abc", "ac", "abcd"]
        return stringList
    
    def wordsInListsCounter():
        elements = listOfWords(list)
    
        if len(elements) != 0:
             strLessThanThreshold = [x for x in elements if len(x) <= threshold]
             return strLessThanThreshold
    
        elif len(elements) == 0:
            emptyString = "There are no words in this list"
            return emptyString
    
        else:
            error = "There is invalid information"
            return error
    
    print(wordsInListsCounter())
    

    但是,如果您不想使用列表推导式,则可以使用以下内容:

    threshold = 2
    def listOfWords(list):
        stringList = ["a", "ab", "abc", "ac", "abcd"]
        return stringList
    
    def wordsInListsCounter():
        strLessThanThreshold = []
        elements = listOfWords(list)
    
        for element in elements :
            if len(element) <= threshold:
                strLessThanThreshold.append(element)
    
        if len(elements) == 0:
            emptyString = "There are no words in this list"
            return emptyString
    
        return strLessThanThreshold
    
    print(wordsInListsCounter())
    

    【讨论】:

    • 现在我可以看到如何使用它了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    相关资源
    最近更新 更多