views:

176

answers:

3

I'm currently writing a program in Python to track statistics on video games. An example of the dictionary I'm using to track the scores :

ten = 1
sec = 9
fir = 10
thi5 = 6
sec5 = 8

games = {
        'adom': [ten+fir+sec+sec5, "Ancient Domain of Mysteries"],
        'nethack': [fir+fir+fir+sec+thi5, "Nethack"]
        }

Right now, I'm going about this the hard way, and making a big long list of nested ifs, but I don't think that's the proper way to go about it. I was trying to figure out a way to sort the dictionary, via the arrays, and then, finding a way to display the first ten that pop up... instead of having to work deep in the if statements.

So... basically, my question is : Do you have any ideas that I could use to about making this easier, instead of wayyyy, way harder?

===== EDIT ====

the ten+fir produces numbers. I want to find a way to go about sorting the lists (I lack the knowledge of proper terminology) to go by the number (basically, whichever ones have the highest number in the first part of the array go first.

Here's an example of my current way of going about it (though, it's incomplete, due to it being very tiresome : Example Nests (paste2) (let's try this one?)

==== SECOND EDIT ====

In case someone doesn't see my comment below :

ten, fir, et cetera - these are just variables for scores. Basically, it goes from a top ten list into a variable number. ten = 1, nin = 2, fir = 10, fir5 = 10, sec5 = 8, sec = 9... so : 'adom': [ten+fir+sec+sec5, "Ancient Domain of Mysteries"] actually registers as : 'adom': [1+10+9+8, "Ancient Domain of Mysteries"] , which ends up looking like :

'adom': [28, "Ancient Domain of Mysteries"]

So, basically, if I ended up doing the "top two" out of my example, it'd be :

((1)) Nethack (48)

((2)) ADOM (28)

I'd write an actual number, but I'm thinking of changing a few things up, so the numbers might be a touch different, and I wouldn't want to rewrite it.

== THIRD (AND HOPEFULLY THE FINAL) EDIT ==

Fixed my original code example.

+3  A: 

How about something like this:

scores = games.items()
scores.sort(key = lambda key, value: value[0])
return scores[:10]

This will return the first 10 items, sorted by the first item in the array. I'm not sure if this is what you want though, please update the question (and fix the example link) if you need something else...

Wim
+1  A: 
import heapq

return heapq.nlargest(10, games.iteritems(), key=lambda k, v: v[0])

is the most direct way to get the top ten key / value pairs, sorted by the first item of each "value" list. If you can define more precisely what output you want (just the names, the name / value pairs, or what else?) and the sorting criterion, this is easy to adjust, of course.

Alex Martelli
A: 

Wim's solution is good, but I'd say that you should probably go the extra mile and push this work off onto a database, rather than relying on Python. Python interfaces well with most types of databases, where much of what you're exploring is already a solved problem.

For example, instead of worrying about shifting your dictionaries to various other data types in order to properly sort them, you can simply get all the data for each pertinent entry pre-sorted based on the criteria of your query. There goes the need for convoluted sorting and resorting right there.

While dictionaries are tempting to use, because they give the illusion of database-like abilities to access data based on its attributes, I still think they stumble quite a bit with respect to implementation. I don't really have any numbers to throw at you, but just from personal experience, anything you do on Python when it comes to manipulating large amounts of data, you can do much faster and more efficient both in code and computation with something like MySQL.

I'm not sure what you have planned as far as the structure of your data goes, but along with adding data, changing its structure is a lot easier using a database, too.

Robert Elwell
Define **large** ;-) Up to a few hundred items, I'm guessing performance will be very similar with a Python-only implementation while saving you a lot of setup headackes. It all depends(tm) on a lot of environmental conditions we don't know about however...
Wim
-1: Disagree. Python has numerous advantages over databases. Python dictionaries, lists, and the `sorted` function will do this nicely without adding the complexity of SQL. A database helps when the data needs to have concurrent access. Otherwise, Python structures work wonderfully for any database that fits in memory.
S.Lott
Databases incur much overhead.
recursive
I suppose my bias is _for_ concurrent usage. Given the aforementioned use case, it wouldn't make sense not to at least plan for extensibility and concurrent usage in the future. Why else would you make such a program for a single individual on a single machine? This sounds like a component to a web application.
Robert Elwell
Maybe, or maybe it's just a quick and dirty script to do some offline processing on a ranking he scraped from the 'net, we don't know... But your suggestion is certainly valid in the general sense so I'll +1 you back to 0.
Wim