【发布时间】: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