Sorting items in dictionary
Posted: Sat Jan 25, 2014 6:02 pm
I have a dictionary which contains a steamid, which is storing other dictionaries for attributes of a player. I am trying to sort these values to create a leader board, but am not getting the results I expect (sometimes, they're not displayed in order).
The specific attribute which should be sorted by is entered into the form. Values you could use to test are "fighter, cleric, wizard, rogue". Fighter seems to work flawlessly, but the issue I notice is with "cleric". Test page: http://thinkplace.tk/dnd/kills.py
http://pastebin.com/3Gj1tYpn
The specific attribute which should be sorted by is entered into the form. Values you could use to test are "fighter, cleric, wizard, rogue". Fighter seems to work flawlessly, but the issue I notice is with "cleric". Test page: http://thinkplace.tk/dnd/kills.py
http://pastebin.com/3Gj1tYpn
Syntax: Select all
import cgi, cgitb
import pickle, os
cgitb.enable()
print 'Content-Type: text/html\n'
print '<html><body>Class Leaderboards<p>'
myForm = '<form method="get" action="kills.py"> Enter a Class to get people with the highest levels:<p> <input type="text" name="Class"><input type="submit" value="Submit"></form>'
form = cgi.FieldStorage()
Class = form.getfirst('Class', 'fighter')
Class = cgi.escape(Class)
playerdata = {}
filename = 'playerdict.txt'
dndpath = './'
dndpath = dndpath + filename
if os.path.isfile(dndpath):
file_users = open(dndpath, 'rb')
try:
playerdata = pickle.load(file_users)
except:
pass
file_users.close()
else:
print 'Couldn\'t find file %s'%dndpath
myList = []
tempData = playerdata.copy()
currentPlayer = 0
print myForm
sortClass = Class.lower()
for z in range(10):
start = 0
for x,y in tempData.iteritems():
if sortClass in tempData[x]:
if start == 0:
currentPlayer = x
start = 1
if tempData[currentPlayer][sortClass] < tempData[x][sortClass]:
currentPlayer = x
myList.append(currentPlayer)
if currentPlayer in tempData:
del tempData[currentPlayer]
for x in myList:
if x in playerdata:
if sortClass in playerdata[x]:
print 'Name: %s | Level: %s
'%(playerdata[x]['name'], playerdata[x][sortClass])
else:
print '%s not found'%x
print "</body></html>"