Issue
DESIRED OUTPUT:
Proportion of 1- letter words: 4.76% (1231 words)
Proportion of 2- letter words: 16.14% (4177 words)
Proportion of 3- letter words: 20.33% (5261 words)
Proportion of 4- letter words: 24.33% (6295 words)
Proportion of 5- letter words: 15.03% (3889 words)
Proportion of 6- letter words: 7.91% (2048 words)
Proportion of 7- letter words: 5.22% (1352 words)
Proportion of 8- letter words: 3.68% (953 words)
Proportion of 9- letter words: 1.46% (378 words)
Proportion of 10- letter words: 0.73% (190 words)
Proportion of 11- letter words: 0.27% (71 words)
Proportion of 12- letter words: 0.08% (20 words)
Proportion of 13- letter words: 0.04% (10 words)
Proportion of 14- letter words: 0.01% (2 words)
MY CODE:
file = open('romeo_and_juliet_data.txt','r')
mydict = {}
wordcount = 0
proportion = 0
for line in file:
line = line.strip()
line = line.split()
for word in line:
word = word.replace("'",'')
wordlength = len(word)
wordcount+= 1
if wordlength in mydict:
mydict[wordlength] += 1
else:
mydict[wordlength] =1
for value in mydict.values():
proportion = list(mydict.values())
for key,value in mydict.items():
mydict[key] = round((value/wordcount)*100,2)
for key,value in mydict.items():
mydict[key] = f'Proportion of {key}- letter words: {value}%'
for value in mydict.values():
print(value)
print(proportion)
The output my code gives me:
Proportion of 5- letter words: 15.03%
Proportion of 3- letter words: 20.33%
Proportion of 6- letter words: 7.91%
Proportion of 1- letter words: 4.76%
Proportion of 8- letter words: 3.68%
Proportion of 10- letter words: 0.73%
Proportion of 4- letter words: 24.33%
Proportion of 2- letter words: 16.14%
Proportion of 7- letter words: 5.22%
Proportion of 13- letter words: 0.04%
Proportion of 11- letter words: 0.27%
Proportion of 9- letter words: 1.46%
Proportion of 12- letter words: 0.08%
Proportion of 14- letter words: 0.01%
[3889, 5261, 2048, 1231, 953, 190, 6295, 4177, 1352, 10, 71, 378, 20, 2]
How can I get the elements from the list next to the print out of the dictionary values?
I tried doing:
file = open('romeo_and_juliet_data.txt','r')
mydict = {}
wordcount = 0
proportion = 0
for line in file:
line = line.strip()
line = line.split()
for word in line:
word = word.replace("'",'')
wordlength = len(word)
wordcount+= 1
if wordlength in mydict:
mydict[f'Proportion of {wordlength}- letter words:'] += 1
else:
mydict[f'Proportion of {wordlength}- letter words:'] =1
for value in mydict.values():
proportion = list(mydict.values())
for key,value in mydict.items():
mydict[key] = round((value/wordcount)*100,2)
for key,value in mydict.items():
mydict[key] = f'{value}%'
print(mydict)
but for some reason the f string makes the if statement not work and it doesnt add the counts, just adds 1 and then stops. im not sure why changing the mydict key to a f string makes it behave different than if it was just [wordlength]
Solution
This should get you on the right track...
In your data capture, just store the number, not the whole string. Much easier to work with.
When you access the data for printing, you can sort the keys (as shown) to get them in order.
You can compute the proportion from the value as shown.
file = open('romeo_and_juliet_data.txt','r')
mydict = {}
wordcount = 0
# proportion = 0
# step 1: gather the data...
for line in file:
line = line.strip()
line = line.split()
for word in line:
word = word.replace("'",'')
wordlength = len(word)
wordcount+= 1
if wordlength in mydict:
mydict[wordlength] += 1
else:
mydict[wordlength] = 1
# for value in mydict.values():
# proportion = list(mydict.values())
# step 2: process the data
# make a sorted list of the keys and use that to access the data
for key in sorted(mydict.keys()):
count = mydict[key]
# compute the portion "on the fly"
proportion = round((count/wordcount)*100,2)
# print it...
print(f'Proportion of {key} letter words: {proportion} ({count})')
Answered By - AirSquid
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.