I want to find the lengths of words in sentences, and return the results as a list of lists.
So something like
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']
should become
[[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]]
Here's the code
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)
What comes out is the results of the last sentence, with each length in its own list.
[[3], [2], [3], [5], [2], [3], [5]]
Any idea where I'm goofing up?
x.splitinstead ofi.splitin your loop, your code works absolutely fine and gives the right result.