943,852 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2339
  • Python RSS
Nov 8th, 2008
0

Ordered Dictionaries?

Expand Post »
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:
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 10
Solved Threads: 2
Newbie Poster
OutOfReach is offline Offline
19 posts
since Aug 2008
Nov 8th, 2008
0

Re: Ordered Dictionaries?

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:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Nov 8th, 2008
0

Re: Ordered Dictionaries?

There is an ordered dictionary module that can be found here. Works just like a regular dictionary but is kept in order.

HTH
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Nov 8th, 2008
0

Re: Ordered Dictionaries?

sort() did sort out the dict, but not in the correct order.
So unfortunately, I am forced to use a list which works nicely. :/
Reputation Points: 10
Solved Threads: 2
Newbie Poster
OutOfReach is offline Offline
19 posts
since Aug 2008
Nov 8th, 2008
0

Re: Ordered Dictionaries?

Dictionary follow a hash order to speed up lookup.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: help need on file reading and writing in python
Next Thread in Python Forum Timeline: python+apache+gnome-open





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC