简体   繁体   English

python以顺序网格格式打印字典元组

[英]python print dictionary tuples in order grid format

i have a 10x10 grid. 我有一个10x10的网格。 this grid is in a dictionary called p_w. 此网格在名为p_w的字典中。 when i print out p_w i get this: 当我打印出p_w我得到这个:

{(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. PS。 im still quite new to python 我对python还是很陌生

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, eg 我看到大多数人都推荐OrderedDict ,但是我认为这对于单单print可能会过大了-就个人而言,我宁愿将print p_w替换为例如

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!). (如果出于某些奇怪的原因需要大括号和逗号,请在打印件上添加大括号和逗号;如果这是显示网格的更自然的方式,请切换x和y;以此类推-这只是一般性的想法!)。

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])

我将使用OrderedDict OrderedDict (您可以在此处看到更多信息:在此处输入链接描述能否解决您的问题,否则像您所说的for循环是我想到的唯一其他选择。:-)

You need to use OrderedDict : 您需要使用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)])

Sort the existing dict by key and use OrderedDict to maintain order. 按键对现有字典进行排序,并使用OrderedDict维护订单。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM