| | |
Ordered Dictionaries?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2008
Posts: 19
Reputation:
Solved Threads: 2
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:
Notice that they are in proper order.
Now, take a look at what happens when I try to print it:
It gets out of order.
My main question is:
How do I stop python from ordering this dictionary, if it even is?
Thanks!
Python Syntax (Toggle Plain Text)
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)
{(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!
You have no choice when it comes to ordering dictionaries. If you want it to print nicely then you CAN order
Hope that helps.
gameTable.keys() . Here is how i would do it: python Syntax (Toggle Plain Text)
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} keys = gameTable.keys() keys.sort() for key in keys: print key print gameTable[key]
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Check out my Site | and join us on IRC | Python Specific IRC
There is an ordered dictionary module that can be found here. Works just like a regular dictionary but is kept in order.
HTH
HTH
![]() |
Similar Threads
- Array Distances (Python)
Other Threads in the Python Forum
- Previous Thread: help need on file reading and writing in python
- Next Thread: python+apache+gnome-open
Views: 979 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






