我想查找句子中单词的长度,并将结果作为列表列表返回。列表列表的Python函数
因此,像
lucky = ['shes up all night til the sun', 'shes up all night for the fun', 'hes up all night to get some', 'hes up all night to get lucky']
应该成为
[[4, 2, 3, 5, 3, 3, 3], [4, 2, 3, 5, 3, 3, 3], [3, 2, 3, 5, 2, 3, 4], [3, 2, 3, 5, 2, 3, 5]]
下面的代码
result =[]
def sentancewordlen(x)
for i in x:
splitlist = x.split(" ")
temp=[]
for y in splitlist:
l = len(y)
temp.append(l)
result.append(temp)
sentancewordlen(lucky)
出来的东西是最后一句的结果,在其自己的每个长度名单。
[[3], [2], [3], [5], [2], [3], [5]]
任何想法,我很愚蠢吗?
除此之外,你叫'x.split',而不是'i.split'在循环的事实,你的代码作品绝对好,并给出正确的结果。 –