I was going through the code on this question for the second answer... counting letter frequency with a dict
word_list=['abc','az','ed']
def count_letter_frequency(word_list):
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
return letter_frequency
I don't understand how keys = letter_frequency.keys()
connects to the rest of the problem.
I tried playing around with the code. I understand it as follows:
word_list=['abc','az','ed']
def count_letter_frequency(word_list): #function name and input
letter_frequency={} #empty dictionary
for word in word_list: #go through word_list for each item, the
first being 'abc'
for letter in word: #from 'abc', loop through each letter...'a',
'b', 'c'
keys=letter_frequency.keys() # where I'm stuck. i read it as our empty dictionary,
letter_fequency, plus keys(), together defined by the
variable 'keys'. make keys out of the letters in
letter_frequency (then count them using the next if/else
statement)
if letter in keys: # how can a letter be in keys? there isn't anything in
letter_frequency either, is there?
letter_frequency[letter] =1
else:
letter_frequency[letter]=1
return letter_frequency
CodePudding user response:
The condition on if letter in keys
, appart from being inefficient and overly complex, is meant to determine if the letter should add one to the existing entry in the dictionary or add a new entry for the first occurrence of each letter. Checking if letter in letter_frequency
would have done the same thing.
A simpler and more efficient approach would be to let the dictionary indexing work for us (as opposed to a sequential search in keys):
word_list=['abc','az','ed']
letter_frequency = dict()
for word in word_list:
for letter in word:
letter_frequency[letter] = letter_frequency.get(letter,0) 1
print(letter_frequency)
{'a': 2, 'b': 1, 'c': 1, 'z': 1, 'e': 1, 'd': 1}
CodePudding user response:
letter_frequency={}
is an empty dictionary.
We then look at each word, one letter at a time.
for letter in word:
We find the keys, which is initially empty:
keys=letter_frequency.keys()
We then consider the current letter:
if letter in keys:
letter_frequency[letter] =1
else:
letter_frequency[letter]=1
First time round, letter
isn't there, since keys
is empty, so we use the else
to add the letter, with a count of one.
letter_frequency[letter]=1
When we look at the next letter, the previous letter is in the keys.
CodePudding user response:
In your above code, keys = letter_frequency.keys()
is used to get the list of keys present in the dictionary such as ['a','b','c']. This is to check whether the key is already present in the dictionary or not.
In my opinion, one should use word_list.get()
function of dictionary to check the availability of a character in a dictionary.
Refer the below code for clarification.
def countLetterFrequency(word_list):
letter_frequency = {}
for word in word_list:
for ch in word:
# use .get() function instead of .keys() to check the presence of character in dictionary.
if not letter_frequency.get(ch, False):
letter_frequency[ch] = 1
else:
letter_frequency[ch] = 1
return letter_frequency
word_list = ['abc', 'az', 'ed']
print(countLetterFrequency(word_list))
Output:
{'a': 2, 'b': 1, 'c': 1, 'z': 1, 'e': 1, 'd': 1}
Note: .get()
function takes the character in first argument
which needs to be checked in the dictionary and in second argument takes the default
output which needs to be return when the key is not present.
CodePudding user response:
In the first iteration there will be nothing in letter_frequency, therefore the else condition of if letter in keys
triggers. In the line letter_frequency[letter]=1
, just above the return, the current letter
will be used as a new key for the empty dict and its value will be set to 1.
Then, in the next iteration of the inner for loop, keys=letter_frequency.keys()
will not be empty anymore.