英文:
Counting the length of each element inside a list
问题
我的任务是仅打印长度小于4个字符的单词。我已尝试了无数种方法,但总是出现错误。非常感谢任何帮助。
b = find_short_words(['dog', 'cat', 'turtle', 'horse', 'pangolin'])
short_words = [word for word in b if len(word) < 4]
尝试使用len函数的循环,但总是返回错误。
英文:
My task is to print only the words that are shorter than 4 characters. I have tried it countless ways, but there is always an error. Would very much appreciate any help.
b = find_short_words(['dog', 'cat', 'turtle', 'horse', 'pangolin'])
short_words = [word for word in b if len(name)]<5
Tried loops with len function, but it always returns an error.
答案1
得分: 2
您正在将一个列表与一个整数进行比较,这是无效的。
words = ['dog', 'cat', 'turtle', 'horse', 'pangolin']
short_words = [word for word in words if len(word) < 5] # 将<5放入循环内
print(short_words)
英文:
You are comparing a list with a integer, which is invalid.
words = ['dog', 'cat', 'turtle', 'horse', 'pangolin']
short_words = [word for word in words if len(word) < 5] # put <5 inside loop
print(short_words)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论