[Tutor] Multiple identical keys in a dictionary

wheelege wheelege@tsn.cc
Sun, 27 May 2001 02:24:28 +1000


This is a multi-part message in MIME format.

------=_NextPart_000_0205_01C0E654.27F3BBE0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

  Hi all,

  I'm implementing a high score table for this game I'm writing in =
python (tkinter) and I thought it would be nice to use a dictionary for =
all the high scores - with the score as the key.
  A rather annoying problem has presented itself in that if somebody =
scores the same as another then one of the two (or however many are =
equal) is kept and the rest overwritten.  The reason I had thrown out a =
list was because I want to store things other than just the score (such =
as name, highest level reached, difficulty level).
  The same thing will happen if I use strings and convert to int then =
sort, then convert back to a string for updating the dict.
  Anyway, the essence of the problem is...

>>> d =3D {100: ['Jim', 'Hard', 10], 50: ['Bob', 'Easy', 6], 20: =
['Fred', 'Silly', 2]}
>>> d[50] =3D ['Darren', 'Medium', 3]
>>> d
{100: ['Jim', 'Hard', 10], 20: ['Fred', 'Silly', 2], 50: ['Darren', =
'Medium', 3]}

  And I'm probably asking for the impossible, but what I'd like instead =
is...

>>> d =3D {100: ['Jim', 'Hard', 10], 50: ['Bob', 'Easy', 6], 20: =
['Fred', 'Silly', 2]}
>>> d[50] =3D ['Darren', 'Medium', 3]
>>> d
{100: ['Jim', 'Hard', 10], 20: ['Fred', 'Silly', 2], 50: ['Darren', =
'Medium', 3], 50: ['Bob', 'Easy', 6]}
>>>=20

  So I'm left thinking - what other data structure to use?  A class for =
just the scores seems a little excessive...especially as I'm already =
using a class to handle all the reading and writing to the actual file =
and things like that.

  Thanks,
  Glen.

------=_NextPart_000_0205_01C0E654.27F3BBE0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 5.50.4522.1800" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV>&nbsp; Hi all,</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; I'm implementing a high score table for this game I'm =
writing in=20
python (tkinter) and I thought it would be nice to use a dictionary for =
all the=20
high scores - with the score as the key.</DIV>
<DIV>&nbsp; A rather annoying problem has presented itself in that if =
somebody=20
scores the same as another then one of the two (or however many are =
equal) is=20
kept and the rest overwritten.&nbsp; The reason I had thrown out a list =
was=20
because I want to store things other than just the score (such as name, =
highest=20
level reached, difficulty level).</DIV>
<DIV>&nbsp; The same thing will happen if I use strings and convert to =
int then=20
sort, then convert back to a string for updating the dict.</DIV>
<DIV>&nbsp; Anyway, the essence of the problem is...</DIV>
<DIV>&nbsp;</DIV>
<DIV>&gt;&gt;&gt; d =3D {100: ['Jim', 'Hard', 10], 50: ['Bob', 'Easy', =
6], 20:=20
['Fred', 'Silly', 2]}<BR>&gt;&gt;&gt; d[50] =3D ['Darren', 'Medium',=20
3]<BR>&gt;&gt;&gt; d<BR>{100: ['Jim', 'Hard', 10], 20: ['Fred', 'Silly', =
2], 50:=20
['Darren', 'Medium', 3]}<BR></DIV>
<DIV>&nbsp; And I'm probably asking for the impossible, but what I'd =
like=20
instead is...</DIV>
<DIV>&nbsp;</DIV>
<DIV>&gt;&gt;&gt; d =3D {100: ['Jim', 'Hard', 10], 50: ['Bob', 'Easy', =
6], 20:=20
['Fred', 'Silly', 2]}<BR>&gt;&gt;&gt; d[50] =3D ['Darren', 'Medium',=20
3]<BR>&gt;&gt;&gt; d<BR>{100: ['Jim', 'Hard', 10], 20: ['Fred', 'Silly', =
2], 50:=20
['Darren', 'Medium', 3], 50: ['Bob', 'Easy', 6]}<BR>&gt;&gt;&gt; </DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;So I'm left thinking - what other data structure to =
use?&nbsp;=20
A class for just the scores seems a little excessive...especially as I'm =
already=20
using a class to handle all the reading and writing to the actual file =
and=20
things like that.</DIV>
<DIV>&nbsp;</DIV>
<DIV>&nbsp; Thanks,</DIV>
<DIV>&nbsp; Glen.</DIV></BODY></HTML>

------=_NextPart_000_0205_01C0E654.27F3BBE0--