0

I am a first-time learner of python, I understand how to use word frequencies to count the number of each unique variable of a list, like this

sentence = ['hello', 'people', 'are', 'the', 'most', 'common', 'word', 'people', 'use', 'for', 'language ', 'learning']

words_freq ={} #dictionary for the counts
for word in sentence:
    if word not in words_freq:

        words_freq[word] =1
    else:

        words_freq[word] +=1

print (words_freq)

however, I wonder how could a word frequencies do on the dictionary by using a double for loop?

for example, I have a dictionary like this

Food = {
2015: ["Apple", "Milk", "Cookie", "Banana", "Orange" ],
2016: ["Potato", "Orange", "Chocolate", "Milk", "Mango"],
2017: ["Fish", "Potato", "Orange", "Mango", "Banana"],
2018: ["Beef", "Pork", "Fish", "Apple", "Cookie"],
2019: ["Pork", "Orange", "Apple", "Mango", "Chocolate"]
}

how to do a word frequencies/count and print something like this? Or store the highest value in list form ? apple : 3 milk : 2 orange : 3 .. .. ..

5
  • 3
    You have the right idea. You need two for loops. I suggest you take a stab at it and figure out as much as you can on your own. You should even consider creating a function to wrap your existing code that counts word frequencies in a list. Commented Feb 17, 2020 at 21:33
  • what's the desired output? Commented Feb 17, 2020 at 21:37
  • the desired output is list out as all type of food and follows with their number in total, and store it in a new dictionary. So I can print it out Commented Feb 17, 2020 at 21:42
  • Your result (a dictionary grouped by count) is off! There are lots of items that appear more than once. That cannot happen in a real dictionary. If you print it, you will see it contains a lot less items. While not an error, it still makes helping you (with, apparently per the already provided answers, a ready-made piece of code...) a lot harder. In your preface you mention a word count; your example output can never be made with such a counting script. Please adjust the desired output. Commented Feb 17, 2020 at 21:57
  • 1
    (There seems to be some misunderstanding about your question. Please edit and clarify. My impression was that from your (existing) word count list you wanted to create a dictionary such as the Food one you show. If that is not the case, please reword.) Commented Feb 17, 2020 at 22:02

5 Answers 5

1

defaultdict would be pretty elegant for your use case it create a default dictionary for a given type for int - the default value is 0 for int value, it enable you to write less code

https://docs.python.org/3.3/library/collections.html#collections.defaultdict

from collections import defaultdict

def get_freq(food_dict: dict) -> dict:
    freq = defaultdict(int)
    for year, lst in food_dict.items():
        for elem in lst:
            freq[elem] += 1
    return freq
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot ! I will try my best to understand this !
1

Using a standard python dictionary you can make use of get(key[, default]) to deal with the case when the key you want to increment does not exist in the dictionary.

get(key[, default])

Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.

food = {
    2015: ["Apple", "Milk", "Cookie", "Banana", "Orange"],
    2016: ["Potato", "Orange", "Chocolate", "Milk", "Mango"],
    2017: ["Fish", "Potato", "Orange", "Mango", "Banana"],
    2018: ["Beef", "Porn", "Fish", "Apple", "Cookie"],
    2019: ["Pork", "Orange", "Apple", "Mango", "Chocolate"]
}

counts = {}
for year in food:
    for item in food[year]:
        counts[item] = counts.get(item, 0) + 1

print(counts)

Output:

{'Apple': 3, 'Milk': 2, 'Cookie': 2, 'Banana': 2, 'Orange': 4, 'Potato': 2, 'Chocolate': 2, 'Mango': 3, 'Fish': 2, 'Beef': 1, 'Porn': 1, 'Pork': 1}

1 Comment

thanks a lot, I just wondering why I got a key error from the second for loop. This helps a lot
0

It helps you:

Food = {
2015: ["Apple", "Milk", "Cookie", "Banana", "Orange" ],
2016: ["Potato", "Orange", "Chocolate", "Milk", "Mango"],
2017: ["Fish", "Potato", "Orange", "Mango", "Banana"],
2018: ["Beef", "Porn", "Fish", "Apple", "Cookie"],
2019: ["Pork", "Orange", "Apple", "Mango", "Chocolate"]
}

words_freq ={} #dictionary for the counts
for word1 in Food.values():
    for word in word1:
        if word not in words_freq:

            words_freq[word] =1
        else:

            words_freq[word] +=1

print (words_freq)

5 Comments

@usr2564301 user shows codes and ask to expand it to nested dict, so I modified, We know that there are many way to do this. In your opinion I should comment behind any answer It will not.
No, what OP is asking is "how to do a word frequencies/count and print something like this?", with that shown dictionary Food as output. (Unless I am very mistaken. OP might want to clarify the question ...)
@usr2564301 No, you didn't understood the question, and If you check the answer that CALVIN accepted, has the same output as mine !
I've added a comment for the OP for clarification. The whole "word count" preamble and sample code seems to have nothing to do with the Food output.
@usr2564301 Food is not the output. It is another dictionary, asking (quite unclearly) how to do a word count across all of its values. The example output (again, quite vaguely) is right in the end of the post
0

You don't need a double loop, but you can do something like this:

food = {
    2015: ["Apple", "Milk", "Cookie", "Banana", "Orange" ],
    2016: ["Potato", "Orange", "Chocolate", "Milk", "Mango"],
    2017: ["Fish", "Potato", "Orange", "Mango", "Banana"],
    2018: ["Beef", "Porn", "Fish", "Apple", "Cookie"],
    2019: ["Pork", "Orange", "Apple", "Mango", "Chocolate"]
}

words_freq = {}
for year in food.keys():
    for fruit in food[year]:

        if fruit in words_freq.keys():
            words_freq[fruit] += 1
        else:
            words_freq[fruit] = 1

Comments

0

Another way you could do this is to create a single list, and then count frequencies in that list:

foodList = []

#creates a single list
for idx, f in enumerate(Food):
    foodList = foodList + list(Food.values())[idx]

#makes a count in that list
words_freq ={}
for word in foodList:
    if word not in words_freq:
        words_freq[word] =1
    else:
        words_freq[word] +=1

which yields the correct answer:

{'Apple': 3, 'Milk': 2, 'Cookie': 2, 'Banana': 2, 'Orange': 4, 'Potato': 2, 'Chocolate': 2, 'Mango': 3, 'Fish': 2, 'Beef': 1, 'Pork': 2}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.