Hey all - I’m aware that the solution solves this differently.
Below is my code. To what seems 100% logical and correct to me, Python runs this and while I get the ‘keys’ correct, my values always come out to be incorrect - where am I going wrong?
def frequency_dictionary(words):
counter = 0 #Initialize counter
dictionary = {} #Initialize dictionary
words_length = len(words) #Obtain length of the list that user inputs
for word in words:
for i in range(0, words_length): #Iterate through user inputted list
if word == words[i]: #See if matching at all
counter += 1 #If matching, add +1 to counter
dictionary.update({word: counter}) #Update dictionary
return dictionary
When I test my code with:
print(frequency_dictionary(["apple", "apple", "cat", 1]))
# should print {"apple":2, "cat":1, 1:1}
I get an output of: {‘apple’: 4, ‘cat’: 5, 1: 6}
Thank you in advance. Identifying the problem will show me what area I lack knowledge in.
You have a single integer variable, counter, that is intended to keep track of the frequency of all the different words. Since it is a single value, it cannot be used to track all of them simultaneously.
You can simplify your code, as follows.
Eliminate the variable counter, and just use dictionary to keep a count of the frequencies. It should start out empty, and will ultimately contain one item for each different value in words.
Iterate through all the items in words. If the item is already represented in dictionary, add 1 to the corresponding value, otherwise add the item to dictionary as a new key with an associated value of 1.
Let’s consider n to represent the number of items in words. With this solution, the outer loop executes n times. During each of those iterations, the inner loop executes n times. Accordingly, the inner loop executes a total of n * n times during the entire solution. For four items, that’s not a real problem, however for a large number of items, say 1,000, we would have a total of 1,000,000 iterations of the inner loop. With a more efficient solution, involving a single loop, we could have this down to 1,000 loop iterations, which is n, or one iteration per item in words.
Edited on April 25, 2020, to add some detail to the discussion
If we assign a 0 value on the 1st iteration, as per the Codecademy solution, I would expect the frequencies to all be underestimated by 1. That is not the case, it works but I don’t quite get how/why?
>>> def frequency(u):
s = u.split()
v = {w: 0 for w in set(s)} # unique words initialized to 0
for w in s:
v[w] += 1
return v
>>> frequency('The sky is blue, the sky is big, the sky is high')
{'The': 1, 'sky': 3, 'is': 3, 'blue,': 1, 'the': 2, 'big,': 1, 'high': 1}
>>>
Or, better still, a sorted frequency table in descending order…
>>> def frequency(u):
s = u.split()
v = {w: 0 for w in set(s)}
for w in s:
v[w] += 1
return dict(sorted(v.items(), key=lambda x: x[1], reverse=True))
>>> frequency('The sky is blue, the sky is big, the sky is high')
{'sky': 3, 'is': 3, 'the': 2, 'blue,': 1, 'big,': 1, 'The': 1, 'high': 1}
>>>
I like the different approaches to solving this problem.
Please see my code below. It works fine. I just want to share with you another way of approaching the challenge.
def frequency_dictionary(words):
new_dictionary={}
for word in words:
new_dictionary[word]=words.count(word)
return new_dictionary
I am still struggling with this same question. I don’t get why if it is not in the dictionary we add it to the dictionary with a value of 0 instead of 1.