0

My model returns information about PC games in the following format. The format is game index and game value. This is my sim_sorted.

[(778, 0.99999994), (1238, 0.9999997), (1409, 0.99999905), (1212, 0.99999815)]

I retrieve the information about the game by indexing the database (df_indieGames):

sims_sorted = sorted(enumerate(sims), key=lambda item: -item[1])

results = {}
    for val in sims_sorted[:4]:
        index, value = val[0], val[1]
        results[df_indieGames.game_name.loc[index]] = 
   {
        "Genre":df_indieGames.genre.loc[index],
        "Rating": df_indieGames.score.loc[index],
        "Link": df_indieGames.game_link[index]
    }

However, such a data structure is hard to sort (by Rating). Is there a better way to store the information so retrieval and sorting is easier? Thanks.

Here's the output of results:

{u'Diehard Dungeon': {'Genre': u'Roguelike',
  'Link': u'http://www.indiedb.com/games/diehard-dungeon',
  'Rating': 8.4000000000000004},
 u'Fork Truck Challenge': {'Genre': u'Realistic Sim',
  'Link': u'http://www.indiedb.com/games/fork-truck-challenge',
  'Rating': 7.4000000000000004},
 u'Miniconomy': {'Genre': u'Realistic Sim',
  'Link': u'http://www.indiedb.com/games/miniconomy',
  'Rating': 7.2999999999999998},
 u'World of Padman': {'Genre': u'First Person Shooter',
  'Link': u'http://www.indiedb.com/games/world-of-padman',
  'Rating': 9.0}}

UPDATE

The solution to the problem as suggested by ziddarth is the following:

result = sorted(results.iteritems(), key=lambda x: x[1]['Rating'], reverse=True)
4
  • 1
    check out pandas.pydata.org Commented Oct 5, 2015 at 19:10
  • To sort by rating you could use the lambda technique again sorted(results.iteritems(), key=lambda x: x[1]['Rating']) Commented Oct 5, 2015 at 19:31
  • @ziddarth: That does solve the problem. Could you write it as a solution so I can mark it as done and you can have the points. Thanks! Commented Oct 5, 2015 at 19:50
  • Also, is there a more efficient way to save this information as a data structure? Or is this a good way? Thanks! Commented Oct 5, 2015 at 19:51

1 Answer 1

1

You can sort by rating using code below. The lambda function is called with a tuple whose first element is the dictionary key and the second element is the dictionary value for the corresponding key, so you can use the lambda function to get to any value in the nested dictionary

sorted(results.iteritems(), key=lambda x: x[1]['Rating'])
Sign up to request clarification or add additional context in comments.

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.