0

I am trying to write a script to count the occurrences of an item in a MULTIDIMENSIONAL list. For example it would return:

Happy: 1 Giggly: 3 Hazy: 1

 data = {"AK-47":["Happy","Giggly","Confident"],
"Gelato":["Slumped","Tired","Light","Giggly"],
"Buddah Bliss":["Confused","Euphoria","Hazy"],
"Grandaddy Purple":["Sleepy","Slumped"],
"Laughing Gas":["Giggly","Light","Euphoria"]}

def countX(lst, x):
    count = 0
    for ele in lst:
        if (ele == x):
            count = count + 1
    return count

for key,values in data.items():
    if key == str:
        print (key)
    
    print ('\n'+key)
    for i in values:
        print("-",i)

1 Answers1

0
data = {"AK-47": ["Happy", "Giggly", "Confident"],
        "Gelato": ["Slumped", "Tired", "Light", "Giggly"],
        "Buddah Bliss": ["Confused", "Euphoria", "Hazy"],
        "Grandaddy Purple": ["Sleepy", "Slumped"],
        "Laughing Gas": ["Giggly", "Light", "Euphoria"]}

words = {}

for k in data:  # Iterate over every key in your dictionary
    for v in data[k]:  # Iterate over every value of every key in your dictionary
        if v in words:  # If key already in "words" dictionary, enumerate it
            words[v] += 1
        else:  # Else add the word
            words[v] = 1

print(words)
Omar AlSuwaidi
  • 1,187
  • 2
  • 6
  • 26