【问题标题】:{Python} How can I convert dictionary into Coordinates?{Python} 如何将字典转换为坐标?
【发布时间】:2020-01-13 13:28:48
【问题描述】:
{1: [0, 0], 2: [1, 0], 3: [2, 0], 4: [3, 0], 5: [4, 0], 6: [5, 0], 7: [5, 1], 8: [5, 2], 9: [5, 3], 17: [4, 4], 28: [3, 4], 29: [2, 4], 30: [1, 4], 31: [1, 3], 32: [1, 2], 33: [2, 2], 34: [3, 2], 35: [3,0: [5, 4], 11: [5, 5], 12: [4, 5], 13: [3, 5], 14: [2, 5], 15: [1, 5], 16: [0, 5], 17: [0, 4], 18: [0, 3], 19: [0, 2], 20: [0, 1], 21: [1, 1], 22: [2, 1], 23: [3, 1], 24: [4, 1], 25: [4, 2], 26: [4, 3], 27: [4, 4], 28: [3, 4], 29: [2, 4], 30: [1, 4], 31: [1, 3], 32: [1, 2], 33: [2, 2], 34: [3, 2], 35: [3, 3], 36: [2, 3]}

我有一个像上面这样的字典,前面的数字是值,后面的(列表)是坐标, 如何将字典打印到坐标?

顺便说一下,这是螺旋数组。

(抱歉英语不好。)

我觉得用图片解释比较好

enter image description here

【问题讨论】:

  • @TJCWorld 不,我想打印这样的东西。 . ...... 9 ...... 8 ...... 7 1 2 3 4 5 6 螺旋数字。例如,1 在 (0,0,) 2 在 (1,0)
  • @TJCWorld 抱歉,这是我第一次使用 stackoverflow。我通过图像解释了它。感谢您的评论!

标签: python numpy dictionary coordinates


【解决方案1】:

不得不稍微修正一下你的字典 (d),我认为 10 被删除了。

coords = np.array(list(d.values()))
out = np.zeros(coords.max(0) + 1)
out[tuple(coords.T)] = list(d.keys())

out
Out[]: 
array([[ 1., 20., 19., 18., 17., 16.],
       [ 2., 21., 32., 31., 30., 15.],
       [ 3., 22., 33., 36., 29., 14.],
       [ 4., 23., 34., 35., 28., 13.],
       [ 5., 24., 25., 26., 27., 12.],
       [ 6.,  7.,  8.,  9.,  0., 11.]])

【讨论】:

    【解决方案2】:

    如果您的意思是在笛卡尔轴上绘制坐标:

    coords = list(dict.values())
    
    x = np.array(coords)[:, 0]
    y = np.array(coords)[:, 1]
    
    import matplotlib.pyplot as plt
    
    plt.scatter(x, y)
    plt.show()
    

    如果你只想打印坐标:

    print(dict.values())
    

    【讨论】:

    • 抱歉,原始问题中的表述含糊不清。我添加了一张图片,以帮助您更容易理解。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-05-29
    • 2016-02-13
    • 1970-01-01
    • 2021-09-03
    • 2013-02-14
    • 2021-05-29
    • 2019-12-20
    • 2013-02-19
    相关资源
    最近更新 更多