Reputation: 105
Here's the jist of what I'm trying to accomplish. I have a .txt file(dict.txt) which contain a ton of words. My task is count the frequency of each letter in the .txt file and put it in a list, convert each element into a percentage (divide ea element by 100), then use that list as my y_axis for my bar plot.
So far I've created a dictionary which contains each letter of alphabet as the key and the value equals the total amount of times that letter appears in the .txt file. Where I'm stuck is getting each value to divide by 100, then place that new number into a list where I can use as my y-axis for my plot. the x-axis are the letters themselves.
Here is the code I already wrote:
letter_dict = {}
word_list = []
filename = raw_input('Enter filename: ')
new_file = open(filename).readlines()
for i in new_file:
word = i.strip().lower()
word_list += list(word)
for letter in word_list:
if letter in letter_dict:
letter_dict[letter] += 1
else:
letter_dict[letter] = 1
x_axis = []
y_axis = []
summ= 0
for i in letter_dict.values(): #sum of all values in list
summ += i
value_list = list(letter_dict.values())
for k in letter_dict:
x_axis += [k]
print summ
y_axis = []
num_avg = []
for i in value_list:
y_axis += [int(i) / summ]
create_plot(x_axis, y_axis, filename) #this is for my "plot" function
whenever I for loop (i in value_list) then divide ea element by the sum, the printed list returns as [0,0,0,0,0,0,0,0,0,0,0,0,0,0]. I'm stumped.
Upvotes: 1
Views: 1665
Reputation: 56654
Here is a rewritten version:
# Assumes Python 2.7
from collections import Counter
import matplotlib.pyplot as plt
from string import ascii_lowercase
def get_file():
fname = raw_input("Enter the file name: ")
with open(fname) as inf:
return inf.read()
def count_letters(s):
chars = Counter(s.lower())
return {ch:chars[ch] for ch in ascii_lowercase}
def plot_letters(count):
total = sum(count.values())
xs = range(len(ascii_lowercase))
ys = [count[ch] * 100. / total for ch in ascii_lowercase]
plt.bar(xs, ys)
plt.xticks([x+0.5 for x in xs], ascii_lowercase)
plt.show()
def main():
letters = get_file()
count = count_letters(letters)
plot_letters(count)
main()
which produces something like:
Upvotes: 0
Reputation: 44112
The problem can be here:
y_axis += [int(i) / summ]
Dividing two integers resturns integer, here you get rounded the real result out.
As soon as one of the numbers is e.g. float, you will get float result.
y_axis += [int(i) / float(summ)]
Upvotes: 2
Reputation: 6675
The reason they return as 0 is because Python uses integer division. Use float to get more intuitive results.
In [1]: 1/5
Out[1]: 0
In [2]: float(1)/5
Out[2]: 0.2
Upvotes: 2