0

i have a 10x10 grid. this grid is in a dictionary called p_w. when i print out p_w i get this:

{(7, 3): 0.01, (6, 9): 0.01, (0, 7): 0.01, (1, 6): 0.01, (3, 7): 0.01, (2, 5): 0.01, (8, 5): 0.01, (5, 8): 0.01, (4, 0): 0.01, (9, 0): 0.01, 
(6, 7): 0.01, (5, 5): 0.01, (7, 6): 0.01, (0, 4): 0.01, (1, 1): 0.01, (3, 2): 0.01, (2, 6): 0.01, (8, 2): 0.01, (4, 5): 0.01, (9, 3): 0.01, 
(6, 0): 0.01, (7, 5): 0.01, (0, 1): 0.01, (3, 1): 0.01, (9, 9): 0.01, (7, 8): 0.01, (2, 1): 0.01, (8, 9): 0.01, (9, 4): 0.01, (5, 1): 0.01, 
(7, 2): 0.01, (1, 5): 0.01, (3, 6): 0.01, (2, 2): 0.01, (8, 6): 0.01, (4, 1): 0.01, (9, 7): 0.01, (6, 4): 0.01, (5, 4): 0.01, (7, 1): 0.01, 
(0, 5): 0.01, (1, 0): 0.01, (0, 8): 0.01, (3, 5): 0.01, (2, 7): 0.01, (8, 3): 0.01, (4, 6): 0.01, (9, 2): 0.01, (6, 1): 0.01, (5, 7): 0.01, 
(7, 4): 0.01, (0, 2): 0.01, (1, 3): 0.01, (4, 8): 0.01, (3, 0): 0.01, (2, 8): 0.01, (9, 8): 0.01, (8, 0): 0.01, (6, 2): 0.01, (5, 0): 0.01, 
(1, 4): 0.01, (3, 9): 0.01, (2, 3): 0.01, (1, 9): 0.01, (8, 7): 0.01, (4, 2): 0.01, (9, 6): 0.01, (6, 5): 0.01, (5, 3): 0.01, (7, 0): 0.01, 
(6, 8): 0.01, (0, 6): 0.01, (1, 7): 0.01, (0, 9): 0.01, (3, 4): 0.01, (2, 4): 0.01, (8, 4): 0.01, (5, 9): 0.01, (4, 7): 0.01, (9, 1): 0.01, 
(6, 6): 0.01, (5, 6): 0.01, (7, 7): 0.01, (0, 3): 0.01, (1, 2): 0.01, (4, 9): 0.01, (3, 3): 0.01, (2, 9): 0.01, (8, 1): 0.01, (4, 4): 0.01, 
(6, 3): 0.01, (0, 0): 0.01, (7, 9): 0.01, (3, 8): 0.01, (2, 0): 0.01, (1, 8): 0.01, (8, 8): 0.01, (4, 3): 0.01, (9, 5): 0.01, (5, 2): 0.01}

i am trying to get it so that its print out in order of coordinates. for example

{(0,0):0.01, (0.1):0.01, (0,2):0.01... etc

how do i order the tuples in the dictionary i curreny have:

p_w = {}
for x in range(xwhale):
    for y in range(ywhale):
        p_w[x,y] = 0.01         

self.p_w = p_w

print p_w

PS. im still quite new to python

Ross Watson
  • 65
  • 1
  • 11

5 Answers5

2

I see most everybody's recommending OrderedDict, but I think that's likely overkill for a mere print -- personally, I'd rather replace the print p_w with, e.g

for x in range(xwhale):
    for y in range(ywhale):
        print '(%s,%s): %s' % (x, y, p_[x,y]),
    print

(add braces and commas to the prints if for some weird reason you want them; switch x and y if that's a more natural way to show your grid; etc, etc -- this is just the general idea!).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
1
data = {(7, 3): 0.01, (6, 9): 0.01, (0, 7): 0.01, (1, 6): 0.01, (3, 7): 0.01, (2, 5): 0.01, (8, 5): 0.01, (5, 8): 0.01, (4, 0): 0.01, (9, 0): 0.01, 
(6, 7): 0.01, (5, 5): 0.01, (7, 6): 0.01, (0, 4): 0.01, (1, 1): 0.01, (3, 2): 0.01, (2, 6): 0.01, (8, 2): 0.01, (4, 5): 0.01, (9, 3): 0.01, 
(6, 0): 0.01, (7, 5): 0.01, (0, 1): 0.01, (3, 1): 0.01, (9, 9): 0.01, (7, 8): 0.01, (2, 1): 0.01, (8, 9): 0.01, (9, 4): 0.01, (5, 1): 0.01, 
(7, 2): 0.01, (1, 5): 0.01, (3, 6): 0.01, (2, 2): 0.01, (8, 6): 0.01, (4, 1): 0.01, (9, 7): 0.01, (6, 4): 0.01, (5, 4): 0.01, (7, 1): 0.01, 
(0, 5): 0.01, (1, 0): 0.01, (0, 8): 0.01, (3, 5): 0.01, (2, 7): 0.01, (8, 3): 0.01, (4, 6): 0.01, (9, 2): 0.01, (6, 1): 0.01, (5, 7): 0.01, 
(7, 4): 0.01, (0, 2): 0.01, (1, 3): 0.01, (4, 8): 0.01, (3, 0): 0.01, (2, 8): 0.01, (9, 8): 0.01, (8, 0): 0.01, (6, 2): 0.01, (5, 0): 0.01, 
(1, 4): 0.01, (3, 9): 0.01, (2, 3): 0.01, (1, 9): 0.01, (8, 7): 0.01, (4, 2): 0.01, (9, 6): 0.01, (6, 5): 0.01, (5, 3): 0.01, (7, 0): 0.01, 
(6, 8): 0.01, (0, 6): 0.01, (1, 7): 0.01, (0, 9): 0.01, (3, 4): 0.01, (2, 4): 0.01, (8, 4): 0.01, (5, 9): 0.01, (4, 7): 0.01, (9, 1): 0.01, 
(6, 6): 0.01, (5, 6): 0.01, (7, 7): 0.01, (0, 3): 0.01, (1, 2): 0.01, (4, 9): 0.01, (3, 3): 0.01, (2, 9): 0.01, (8, 1): 0.01, (4, 4): 0.01, 
(6, 3): 0.01, (0, 0): 0.01, (7, 9): 0.01, (3, 8): 0.01, (2, 0): 0.01, (1, 8): 0.01, (8, 8): 0.01, (4, 3): 0.01, (9, 5): 0.01, (5, 2): 0.01}

for coords in sorted(data):  # sorts the keys, data order unchanged 
    print '{0}: {1}'.format(coords, data[coords])
dopstar
  • 1,478
  • 10
  • 20
0

I would use a OrderdDict OrderedDict (You can see more here: enter link description here Does that solve your problem, else a for-loop like you are talking about is the only other option who spring to my mind.. :-)

Community
  • 1
  • 1
Thomas
  • 237
  • 4
  • 15
0

You need to use OrderedDict :

>>> from collections import OrderedDict
>>> a=OrderedDict()
>>> s=sorted(d.items())
>>> for i,j in s:
...  a.update({i:j})
... 
>>> a
OrderedDict([((0, 0), 0.01), ((0, 1), 0.01), ((0, 2), 0.01), ((0, 3), 0.01), ((0, 4), 0.01), ((0, 5), 0.01), ((0, 6), 0.01), ((0, 7), 0.01), ((0, 8), 0.01), ((0, 9), 0.01), ((1, 0), 0.01), ((1, 1), 0.01), ((1, 2), 0.01), ((1, 3), 0.01), ((1, 4), 0.01), ((1, 5), 0.01), ((1, 6), 0.01), ((1, 7), 0.01), ((1, 8), 0.01), ((1, 9), 0.01), ((2, 0), 0.01), ((2, 1), 0.01), ((2, 2), 0.01), ((2, 3), 0.01), ((2, 4), 0.01), ((2, 5), 0.01), ((2, 6), 0.01), ((2, 7), 0.01), ((2, 8), 0.01), ((2, 9), 0.01), ((3, 0), 0.01), ((3, 1), 0.01), ((3, 2), 0.01), ((3, 3), 0.01), ((3, 4), 0.01), ((3, 5), 0.01), ((3, 6), 0.01), ((3, 7), 0.01), ((3, 8), 0.01), ((3, 9), 0.01), ((4, 0), 0.01), ((4, 1), 0.01), ((4, 2), 0.01), ((4, 3), 0.01), ((4, 4), 0.01), ((4, 5), 0.01), ((4, 6), 0.01), ((4, 7), 0.01), ((4, 8), 0.01), ((4, 9), 0.01), ((5, 0), 0.01), ((5, 1), 0.01), ((5, 2), 0.01), ((5, 3), 0.01), ((5, 4), 0.01), ((5, 5), 0.01), ((5, 6), 0.01), ((5, 7), 0.01), ((5, 8), 0.01), ((5, 9), 0.01), ((6, 0), 0.01), ((6, 1), 0.01), ((6, 2), 0.01), ((6, 3), 0.01), ((6, 4), 0.01), ((6, 5), 0.01), ((6, 6), 0.01), ((6, 7), 0.01), ((6, 8), 0.01), ((6, 9), 0.01), ((7, 0), 0.01), ((7, 1), 0.01), ((7, 2), 0.01), ((7, 3), 0.01), ((7, 4), 0.01), ((7, 5), 0.01), ((7, 6), 0.01), ((7, 7), 0.01), ((7, 8), 0.01), ((7, 9), 0.01), ((8, 0), 0.01), ((8, 1), 0.01), ((8, 2), 0.01), ((8, 3), 0.01), ((8, 4), 0.01), ((8, 5), 0.01), ((8, 6), 0.01), ((8, 7), 0.01), ((8, 8), 0.01), ((8, 9), 0.01), ((9, 0), 0.01), ((9, 1), 0.01), ((9, 2), 0.01), ((9, 3), 0.01), ((9, 4), 0.01), ((9, 5), 0.01), ((9, 6), 0.01), ((9, 7), 0.01), ((9, 8), 0.01), ((9, 9), 0.01)])
Mazdak
  • 105,000
  • 18
  • 159
  • 188
0

Sort the existing dict by key and use OrderedDict to maintain order.

from collections import OrderedDict

xwhale = ywhale = 10

p_w = {}
for x in range(xwhale):
    for y in range(ywhale):
        p_w[x,y] = 0.01         

print p_w

op_w = OrderedDict(sorted(p_w.items(), key=lambda t: t[0]))
print '\n\n'
print op_w
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61