简体   繁体   中英

{Python} How can I convert dictionary into Coordinates?

{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]}

I have a dictionary like above, the front numbers are values and the latter(list) are coordiates, How can I print the dictionary to coordinates?

This is sprial array by the way.

(sorry for the bad English.)

I think it is better to explain in picture

enter image description here

If you mean plotting coordinates on a Cartesian axis:

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

If you just want to print the coordinates:

print(dict.values())

Had to fix up your dictionary ( d ) a bit, I think 10 got dropped.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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