I am taking words from a text file, stripping each word of \n and creating a new list out of these words.
Now I need to go through systematically word by word finding the length of the word, then adding 1 to a tally of that word length i.e. I would start off with an empty tally:
length_of_words = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
then if the list of stripped words contained 5x 7 letter words and 3x 2 letter words I would end up with:
length_of_words = [0,3,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
What this boils down to is:
- Calculate the length of a word e.g. n
- Add one to length_of_words for length_of_words[n-1] (as it still starts with 1 letter words being the 0th item)
I am really stuck on how to essentially increase the value of 1 item in a list by 1, instead of just appending 1 to the end of the list.
What I have at the moment is this:
lines = open ('E:\Python\Assessment\dracula.txt', 'r'). readlines () stripped_list = [item.strip() for item in lines] tally =  #empty set of lengths for lengths in range(1,20): tally.append(0) print tally #original tally for i in stripped_list: length_word = int(len(i)) tally[length_word] = tally[length_word] + 1 print tally