Python read from text file VII find percentage letters in words
Using the wordlist, produce a dictionary whose keys are the letters a through z and whose values are the percentage of words that use that letter.
wordlist = [line.strip()
for line
in open('C:/Users/ZM/Desktop/Documents/New
folder/wordlist.txt')]
L = []
for i in wordlist:
i= i.lower()
L.append(i)
d = {}
for i in L:
for w in i:
if w.isalpha():
if w in d:
d[w] += 0
else:
d[w] = 0
D = list(d)
count = 0
for i in range(len(D)):
for s in range(len(L)):
if D[i] in L[s]:
count += 1
d[D[i]] = round(count/len(wordlist)*100, 2)
count = 0
print(d)
L = []
for i in wordlist:
i= i.lower()
L.append(i)
d = {}
for i in L:
for w in i:
if w.isalpha():
if w in d:
d[w] += 0
else:
d[w] = 0
D = list(d)
count = 0
for i in range(len(D)):
for s in range(len(L)):
if D[i] in L[s]:
count += 1
d[D[i]] = round(count/len(wordlist)*100, 2)
count = 0
print(d)
SOLUTION:
{'a': 38.81, 'b': 7.56, 'i': 33.53, 'l': 23.98, 't': 39.0, 'y': 11.64, 'e': 57.61, 'o': 35.72, 'u': 17.01, 'v': 7.16, 'c': 21.79, 'p': 13.93, 'r': 38.91, 'd': 16.82, 'n': 32.04, 'g': 12.54, 's': 26.47, 'm': 14.83, 'f': 9.75, 'h': 17.01, 'w': 8.56, 'k': 4.48, 'x': 1.39, 'z': 0.8, 'j': 0.9, 'q': 0.5}
Коментари
Постави коментар