Python 是否可以将元组坐标存储在嵌套字典中并从嵌套字典中的单个字典访问单个坐标?

Python is it possible to store tuple coordinates in a nested dict and access a single coordinate from a single dict within the nested dict?

我有一些代码使用字典来存储坐标值。我更喜欢有一个键 'R1N1' 的东西,值是 x 和 y 坐标的元组,但我不知道这在 python 中是否可行,或者你将如何索引 a其 x 或 y 分量的键元组:

代码

import json

rnID = dict ([
('R1N1x', 1),
('R1N1y', 111),
('R1N500x', 222),
('R1N500y', 222),
('R2N1x', 1),
('R2N1y', 111),
('R2N500x', 222),
('R2N500y', 222)
])

with open('rnID.txt', 'w') as outfile:
    json.dump(rnID, outfile)

这适用于存储坐标,但有点冗长。

期望输出

something like 
new_dict = {[
('R1N1',(1,111))
...

给你:

rns = {}
for k, v in rnID.items():   
    part = k[-1]
    k = k[:-1]
    if k not in rns:
        rns[k] = [0, 0]
    rns[k][0 if part == 'x' else 1] = v

print(rns)

输出

{'R1N1': [1, 111], 'R1N500': [222, 222], 'R2N1': [1, 111], 'R2N500': [222, 222]}