def find_short(s):
for x in s.split(): # here s.split() generates a list [what, is, your, question]
return min(len(x)) # you will pass the length of the first element because you're only passing length of one word len(what) to min and immediately trying to return.
您需要将iterable项传递给min function not int type
哪里和这里一样
def find_short(s):
return min(len(x) for x in s.split())
#here there are three nested operations:
#1. split the string into list s.split() [what, is, your, question]
#2. loop the list and find the length of each word by len(x) and put it into a tuple compression (4, 2, 4, 8)
#3. pass that tuple compression to the min function min((4, 2, 4, 8))
# and lastly return the smallest element from tuple
min()
接受一个序列,并返回该序列中最小的项。你知道吗len()
获取一个序列,并返回单个长度(该序列的长度)。你知道吗您的第一个代码对每个长度调用
min()
。。。这没有意义,因为min()
需要一个序列作为输入。你知道吗输入:你的问题是什么
您需要将iterable项传递给min function not int type
哪里和这里一样
可能是错的,但我猜你是返回最小长度的x为每x。。。你知道吗
相关问题 更多 >
编程相关推荐