0
我的模型以下列格式返回有关PC游戏的信息。格式是game index
和game value
。这是我的sim_sorted
。如何在Python中将数据最佳保存为数据结构
[(778, 0.99999994), (1238, 0.9999997), (1409, 0.99999905), (1212, 0.99999815)]
我通过索引数据库(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]
}
然而,这样的数据结构是很难区分(按评级)。有没有更好的方法来存储信息,以便检索和排序更容易?谢谢。
这里的输出结果:
{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
的解决问题的方法,通过ziddarth建议如下:
result = sorted(results.iteritems(), key=lambda x: x[1]['Rating'], reverse=True)
检查出http://pandas.pydata.org/ – acushner
要按等级排序,您可以再次使用lambda技术'sorted(results.iteritems(),key = lambda x:x [1] ['Rating'])' – ziddarth
@ziddarth:这确实解决了这个问题。你可以把它写成一个解决方案,这样我就可以将它标记为完成,并且你可以得到这些点。谢谢! – Rohit