Hi! I’m just a beginner in all of these so please don’t judge me if ever my question is too silly or too obvious Any help and feedback will be appreciated.
So I was going through my first guided project regarding “Profitable App Profiles for the App Store and Google Play Markets” and I observed that I have different result in a particular code cell. Specifically, this is the part that calculates the average number of user ratings per app genre on the App Store that the following code:
genres_ios = freq_table(ios_final, -5)
for genre in genres_ios:
total = 0
len_genre = 0
for app in ios_final:
genre_app = app[-5]
if genre_app == genre:
n_ratings = float(app[5])
total += n_ratings
len_genre += 1
avg_n_ratings = total / len_genre
print(genre, ':', avg_n_ratings)
The values of my result (i.e. the average rating per unique genre) are similar to the solution’s result, but the ordering is different. Why is the order different? I attached a screenshot of my result and a solution’s result below.
As per my understanding of your code, the reason could be once you calculate the average number of average ratings then you need to store the values in unsorted dictionary. The next step is to sort the dictionary values into a list of tuples and assign it to new variable then you will the sort values in order as presented in the solution. Please see the below code for your understanding.
genres_ios = freq_table(Ios_final, -5)
unsorted_genres_ios = {}
for genre in genres_ios:
total = 0 # Store sum of ratings for each genre
len_genre = 0 # Store number of apps specific to the genre
for app in Ios_final:
genre_app = app[-5]
if genre_app == genre:
n_ratings = float(app[5])
total += n_ratings
len_genre += 1
avg_n_ratings = total / len_genre # Compute the average number of user rating
unsorted_genres_ios[genre] = avg_n_ratings # Store the value to an unsorted dictionary
Sort the dictionary values into a list of tuples and assign it to a new variable
Hi @imari.borda2018
Regarding your question, you can also refer to this reply.
In addition to that dictionary keys are immutable and keys are always unique, so logically you do not need any fixed index (or ordered index like list) to call value, you can just call value by key irrespective of its position. so no ordering is needed in the dictionary (it’s the default behaviour of dictionary).
Hope it helps!
Let me know if you have further questions.
Thank your very much for your reply! Your code gave me some idea how to otherwise arrange my results! Thanks also for pointing out the “Technical Questions Guidelines”, it’s a very helpful read