El numero dos
El numero dos

Reputation: 51

Counting the words in a dictionary

I have a dictionary which has fruits that were bought that month.

The dictionary looks like this:

bought = {
January: ['Apple', 'Banana', 'Orange', 'Kiwi', 'Raspberry'],
February: ['Orange', 'Mango', 'Banana'], 
March: ['Apple', 'Starfruit', 'Apricot']
}

Is there any way that I could list how many times I bought each fruit.

I want the output to look something like this

Apple: 2, Banana: 2, Orange: 2, Kiwi: 1, Raspberry: 1, Mango: 1, Starfruit: 1, Apricot: 1

All the other places I have use a dictionary to show how many times a word is used in a list. I am looking for a dictionary that finds out how many times a word is mentioned in another dictionary.

Upvotes: 2

Views: 301

Answers (4)

balderman
balderman

Reputation: 23825

Using builtin Counter. One liner :-)

from collections import Counter

bought = {
    "January": ['Apple', 'Banana', 'Orange', 'Kiwi', 'Raspberry'],
    "February": ['Orange', 'Mango', 'Banana'],
    "March": ['Apple', 'Starfruit', 'Apricot']
}

count = Counter([y for x in bought.values() for y in x])
print(count)

Upvotes: 2

Charles Landau
Charles Landau

Reputation: 4275

Your first problem is to get the counts, so we'll restructure into a new dictionary called out:

out = {}
for month in bought:
  for fruit in bought[month]:
    out[fruit] = out.get(fruit, 0) + 1

This for-loop just goes through all the keys (months) in bought and iterates through the list it finds there. As it reads fruits in that list, it checks to see if that fruit is already in out. If not, it initializes that fruit in out as 0. Finally, it increments the value by one.

Now we need to print in the format we want:

for k in out:
      print("{}: {}".format(k, out[k]))

String formatting in Python is extremely mature. You can learn all sorts of tricks at https://pyformat.info/ for example. Here we are just inserting the key and then the value to a string template, as we iterate through out.

Upvotes: 3

Mykola Zotko
Mykola Zotko

Reputation: 17911

You can use Counter:

from collections import Counter
from itertools import chain
from pprint import pprint

c = Counter(chain.from_iterable(bought.values()))

pprint(c)

Output:

Counter({'Apple': 2,
         'Banana': 2,
         'Orange': 2,
         'Kiwi': 1,
         'Raspberry': 1,
         'Mango': 1,
         'Starfruit': 1,
         'Apricot': 1})

Upvotes: 1

PrinceOfCreation
PrinceOfCreation

Reputation: 397

I just quickly threw this together.

Code:

bought = {
"January": ['Apple', 'Banana', 'Orange', 'Kiwi', 'Raspberry'],
"February": ['Orange', 'Mango', 'Banana'], 
"March": ['Apple', 'Starfruit', 'Apricot']
}

numbers = {}
allb = []
for month in bought:
    allb += bought[month]
for item in allb:
    if item in numbers:
        numbers[item] += 1
    else:
        numbers[item] = 1
print(numbers) # Note you can format this however you want: just iterate through the dictionary again

Expected Output:

Apple: 2, Banana: 2, Orange: 2, Kiwi: 1, Raspberry: 1, Mango: 1, Starfruit: 1, Apricot: 1

Actual Output:

{'Apple': 2, 'Banana': 2, 'Orange': 2, 'Kiwi': 1, 'Raspberry': 1, 'Mango': 1, 'Starfruit': 1, 'Apricot': 1}

(As numbers is a dictionary, you can do very powerful things to lookup numbers. It acts like a table in this case.)

The first for loop goes through all the keys in bought (as it is formatted, all the months) and looks up their value. It then adds those values to my list allb (all bought) Next, I iterate through allb and if the item is already in numbers: I add 1 to the count, otherwise I set the count at 1.

Upvotes: 2

Related Questions