Ordered Dictionaries?

Reply

Join Date: Aug 2008
Posts: 19
Reputation: OutOfReach is an unknown quantity at this point 
Solved Threads: 2
OutOfReach OutOfReach is offline Offline
Newbie Poster

Ordered Dictionaries?

 
0
  #1
Nov 8th, 2008
I am creating a tetris-like game. I have decided to use coordinates (x,y) along with a dictionary. Here is a part of the dictionary:
  1. gameTable = {(-5,10):0,(-4,10):0,(-3,10):0,(-2,10):0,(-1,10):0,(0,10):0, (1,10):0, (2,10):0, (3,10):0, (4,10):0, (5,10):0, (-5,9):0, (-4,9):0, (-3,9):0, (-2,9):0, (-1,9):0, (0,9):0, (1,9):0, (2,9):0, (3,9):0, (4,9):0, (5,9):0, (-5,8):0, (-4,8):0, (-3,8):0, (-2,8):0, (-1,8):0, (0,8):0, (1,8):0, (2,8):0, (3,8):0, (4,8):0, (5,8):0...

Notice that they are in proper order.
Now, take a look at what happens when I try to print it:
  1. {(4, -3): 0, (-4, -6): 0, (-1, 0): 0, (-5, 1): 0, (3, -10): 0, (-4, 10): 0, (-5, 3): 0, (0, 4): 0, (-5, -3): 0, (1, 1): 0, (4, 10): 0, (3, 2): 0, (2, 6): 0, (-3, -8): 0, (-4, -3): 0, (-1, -2): 0, (-2, -1): 0, (-4, 4): 0, (2, -10): 0, (2, -7): 0, (-4, -7): 0, (-5, 6): 0, (-1, 3): 0...

It gets out of order.
My main question is:
How do I stop python from ordering this dictionary, if it even is?


Thanks!
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 899
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is online now Online
previously paulthom12345

Re: Ordered Dictionaries?

 
0
  #2
Nov 8th, 2008
You have no choice when it comes to ordering dictionaries. If you want it to print nicely then you CAN order gameTable.keys() . Here is how i would do it:
  1. gameTable = {(-5,10):0,(-4,10):0,(-3,10):0,(-2,10):0,(-1,10):0,(0,10):0, (1,10):0, (2,10):0, (3,10):0, (4,10):0, (5,10):0, (-5,9):0, (-4,9):0, (-3,9):0, (-2,9):0, (-1,9):0, (0,9):0, (1,9):0, (2,9):0, (3,9):0, (4,9):0, (5,9):0, (-5,8):0, (-4,8):0, (-3,8):0, (-2,8):0, (-1,8):0, (0,8):0, (1,8):0, (2,8):0, (3,8):0, (4,8):0}
  2. keys = gameTable.keys()
  3. keys.sort()
  4. for key in keys:
  5. print key
  6. print gameTable[key]
Hope that helps.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 263
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Ordered Dictionaries?

 
0
  #3
Nov 8th, 2008
There is an ordered dictionary module that can be found here. Works just like a regular dictionary but is kept in order.

HTH
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 19
Reputation: OutOfReach is an unknown quantity at this point 
Solved Threads: 2
OutOfReach OutOfReach is offline Offline
Newbie Poster

Re: Ordered Dictionaries?

 
0
  #4
Nov 8th, 2008
sort() did sort out the dict, but not in the correct order.
So unfortunately, I am forced to use a list which works nicely. :/
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,536
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 170
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Ordered Dictionaries?

 
0
  #5
Nov 8th, 2008
Dictionary follow a hash order to speed up lookup.
drink her pretty
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC