Python 3.3.2 - Creating a List of the Length of Words -
i have string of words punctuation, let's example...
string = 'did quick brown fox *really* jump on fence?'
i have filtered out punctuation, now:
'did quick brown fox jump on fence'
and have split list.
list = string.split()
now, list
, need count length of each word list, length of list being longest word. setting out of list follows:
lengthlist = [1_letter_words, 2_letter_words, 3_letter_words, ...]
so, string
, be:
lengthlist = [0, 0, 4, 2, 3, 1]
unfortunately, having trouble doing this. can please provide assistance?
thank you.
i didn't want harangue (at all, not) without giving proper answer, skip ahead if don't care coding practices.
don't use variable names list
, string
because - in case of list
- that's name of type you're making. in fact, that's how make empty instance of type you're making:
something=list() # empty list!
this make confusing reference list[2]
or along lines. didn't hit errors, sake of readability, try come meaningful variable names.
okay, i'm done rant, code you're looking is
st='did quick brown fox jump on fence'.split() c=[len(i) in st] # gives [3, 3, 5, 5, 3, 6, 4, 4, 3, 5] counts=[0]*max(c) # gives [0, 0, 0, 0, 0, 0] in range(len(c)): counts[c[i]-1]+=1 # adds 1 each index of c[i] (we subtract 1 because of 0-based indices) print(counts) # gives answer: [0, 0, 4, 2, 3, 1]
i made of these steps way more advanced challenge you're presenting kind of discourage using in assignment, if happens goal. of tools used in solution @ least a little further ahead of you're working with, if you're learning python reward of understanding code hope illuminating , maybe thinking of radically cool stuff can concisely python. said, let's walk through it:
i'm going assume st
assignment clear enough don't need discuss it, note split right there when assign it. i'm being lazy , in 2 steps, isn't meat of problem let's move on.
c=[len(i) in st]
just means "for each element, we'll call i
, in st
, give me len(i)
in list, , make list c
". might seem daunting, list comprehensions not bad, , can see save quite bit of time in coding. pretty modest implementation of it, really.
counts=[0]*max(c)
says make list 0
s in each space, , make repeat many times max
of c
. take longest word, in case 6-letter word 'really', , make list 6 elements long. ensure have list spaces every length word encounter.
for in range(len(c)): counts[c[i]-1]+=1
oh boy, we're cooking. see we're iterating through list c
, each item through lengths of corresponding words:
- the first element
3
, correspondingdid
. - the second element
3
, correspondingthe
. - ...
- the last element
5
, correspondingfence
.
so that's c[i]
about, counts[c[i]-1]
? counts
going add 1
every length find, it'll add 1
bin when has word 3 characters long. c[i]
give 3
on first element, since lists 0-indexed (lists start @ 0 , goes there), need compensate - hence -1
. see counts[c[i]-1]
, makes little more sense, right?
counts[c[i]-1] # means counts[3-1] means go find bin corresponding counts[2] # ---v 1 [0,0,0,0,0]
and +=1
means "add 1 whatever there already".
python happily iterate through , give answer.
Comments
Post a Comment