Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Explicit is better than implicit.
- # Simple is better than complex.
- # Complex is better than complicated.
- # Above is my textfile which I've opened and added to a list, from the list I add each word into a dictionary. I'm trying to count the amount of words which have a length of 1, 2, 3, 4. In this case: there are 0 char with a length of 1, 3 char with length of 2, 0 char with a length of 3 and 3 char with a length of 4. I'm trying to add this into a dictionary and so the below is the code:
- def words_frequency(filename):
- open_file = open(filename,"r")
- file_text = open_file.read()
- word_list = file_text.split()
- open_file.close()
- word_dict = {}
- for word in word_list:
- if word.isalpha():
- if not word in word_dict:
- if len(word) == 1:
- word_dict[word] = 1
- elif not len(word) == 1:
- word_dict[word] = 0
- if len(word) == 2:
- word_dict[word] = 1
- elif not len(word) == 2:
- word_dict[word] = 0
- if len(word) == 3:
- word_dict[word] = 1
- elif not len(word) == 3:
- word_dict[word] = 0
- if len(word) == 4:
- word_dict[word] = 1
- elif not len(word) == 4:
- word_dict[word] = 0
- else:
- if len(word) == 1:
- word_dict[word] += 1
- if len(word) == 2:
- word_dict[word] += 1
- if len(word) == 3:
- word_dict[word] += 1
- if len(word) == 4:
- word_dict[word] += 1
- #for some reason the output is:
- #{'than': 3, 'Simple': 0, 'is': 2, 'Explicit': 0, 'Complex': 0, 'better': 0}
- #but should be:
- #{'than': 3, 'Simple': 0, 'is': 3, 'Explicit': 0, 'Complex': 0, 'better': 0}
- #what am I doing wrong?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement