【问题标题】:python print dictionary tuples in order grid formatpython以网格格式打印字典元组
【发布时间】:2014-12-21 22:20:19
【问题描述】:

我有一个 10x10 的网格。该网格位于名为 p_w 的字典中。当我打印出 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}

我正在尝试获取它,以便它按坐标顺序打印出来。例如

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

我如何在我现在拥有的字典中排序元组:

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。我对 python 还是很陌生

【问题讨论】:

  • 字典没有任何顺序。检查您使用的ordereddictionary 标签中提供的信息。
  • 你试过OrderedDict吗?

标签: python dictionary tuples ordereddictionary


【解决方案1】:

我看到大多数人都在推荐 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

(如果出于某种奇怪的原因需要它们,则在打印中添加大括号和逗号;如果这是显示网格的更自然的方式,请切换 x 和 y;等等 - 这只是一般的想法!)。

【讨论】:

    【解决方案2】:
    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])
    

    【讨论】:

      【解决方案3】:

      你需要使用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)])
      

      【讨论】:

        【解决方案4】:

        我会使用 OrderdDict OrderedDict (您可以在此处查看更多信息:enter link description here 这是否解决了您的问题,否则像您所说的 for 循环是我想到的唯一其他选择......: -)

        【讨论】:

        • Nvm。使用 OrderedDict 或 @xyres 方式,不要执行 for-for-loop .. :-)
        【解决方案5】:

        对已有的dict进行key排序,使用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
        

        【讨论】:

          猜你喜欢
          • 2017-08-27
          • 2021-04-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-27
          • 1970-01-01
          相关资源
          最近更新 更多