Other way to rank a dictionary based on its values

I am working on the DQ Project and I am trying to make my answer be more presentable and readable by end-users.

The upload above is showing what my actual output is in a dictionary however, I want the str on the left side and the values on the right side. Also I want my values ranked.

My Code:

ios_genres= freq_table(free_ios_apps, -5)
for key in ios_genres:
    total= 0
    len_genre= 0
    for app in free_ios_apps:
        genre_app= app[-5]
        if genre_app == key:
            rating_count= app[5]
            rating_count= float(rating_count)
            total+= rating_count
            len_genre+= 1
    avg_ratings= round(total / len_genre, 2)
    print(key, ":", avg_ratings)
    dictt= {avg_ratings, key}
    #print(dictt)
    sorted(dictt.items(), key= lambda x: x[1])

What I expected to happen: I expected to get a sorted dictt so I can rank my results

What actually happened:

AttributeError: 'set' object has no attribute 'items'

THANK YOU VERY MUCH FRIENDS

Hello!

About the order that you dict displays the data, you cannot expect the dict to show you the str on the left side and the values on the right side if you inform the dict otherwise when you run dictt= {avg_ratings, key}. When you do this, you’re telling python that the key of the dirctionary is the float and the value is the string. Try it like this: dictt = {key, avg_ratings}.

Or you can fill the dictionary during the for loop, like this:

ios_genres= freq_table(free_ios_apps, -5)
for key in ios_genres:
     total= 0
     len_genre= 0
     for app in free_ios_apps:
          genre_app= app[-5]
          if genre_app == key:
               rating_count= app[5]
               rating_count= float(rating_count)
               total+= rating_count
               len_genre+= 1
      
      avg_ratings= round(total / len_genre, 2)
      dict[key] = avg_n_ratings # Here you update the dict

About sorting, dictionaries are unsorted by definition. You cannot use the sort() function on them. In this guided project you are taught to write a function to display your dictionaries in descending order, you should use this function as follows:

def display_table(table):   
     table_display = []
     for key in table:
          key_tuple = (table[key], key)
          table_display.append(key_tuple)
      table_sorted = sorted(table_display, reverse=True)
      for tuple in table_sorted:
          print(tuple[1], ':', tuple[0])

display_table(dict)

Then your output will be something like this:

Navigation : 86090.33333333333
Reference : 74942.11111111111
Social Networking : 71548.34905660378
Music : 57326.530303030304
Weather : 52279.892857142855
Book : 39758.5
Food & Drink : 33333.92307692308
Finance : 31467.944444444445
Photo & Video : 28441.54375
Travel : 28243.8
Shopping : 26919.690476190477
Health & Fitness : 23298.015384615384
Sports : 23008.898550724636
Games : 22788.6696905016
News : 21248.023255813954
Productivity : 21028.410714285714
Utilities : 18684.456790123455
Lifestyle : 16485.764705882353
Entertainment : 14029.830708661417
Business : 7491.117647058823
Education : 7003.983050847458
Catalogs : 4004.0
Medical : 612.0

1 Like