So Lets say I have a dictionary it contains:
{'h': 2, 'e': 1, 'l': 4, 'o': 4, ',': 1, ' ': 3, 'w': 2, 'r': 1, 'd': 1, '!': 1, 'n': 1, 'g': 1, '.': 3, '\n': 1}
and I want to swap my keys and values so that way the frequency is in the front. What would be the best way to do this. I tried this.
my_dict2 = {y: x for x, y in letter_frequency.items()}
and got
{2: 'w', 1: '\n', 4: 'o', 3: '.'}
so it's clearly not working.
Here is my full code to read in the letter frequency. If there is a way to do it without swamping the keys and values that might be best.
word_list = []
file = open(file_name, 'rU')
for line in file:
for word in line:
word_list.append(word);
letter_frequency = {}
for word in word_list:
for letter in word:
keys = letter_frequency.keys()
if letter in keys:
letter_frequency[letter] += 1
else:
letter_frequency[letter] = 1
print(letter_frequency)
my_dict2 = {y: x for x, y in letter_frequency.items()}
print(my_dict2)
return letter_frequency