如何为x创建字典/列表,将y坐标作为键,将数字/字符作为值?

时间:2017-01-06 06:33:30

标签: python list dictionary coordinates

我有这样的数据结构,如下所示:

data = [[(24, 21), (24, 59), (24, 97), (24, 134), (23, 172)],
        [(419, 19), (419, 57), (419, 95), (419, 133), (419, 170), (419, 208), (419, 245)], 
        [(469, 20), (469, 57), (469, 94), (468, 132), (469, 170)]]

我需要这样的结构:

data = [{(24, 21):'1', (24, 59):'2', (24, 97):'3', (24, 134):'4', (23, 172):'0'}, 
        {(419, 19):'1', (419, 57):'2', (419, 95):'3', (419, 133):'4', (419, 170):'0', (419, 208):'x1', (419, 245):'x2'}, 
        {(469, 20):'1', (469, 57):'2', (469, 94):'3', (468, 132):'4', (469, 170):'0'}]

是否有任何代码可以帮助我理清这个结构或动态生成它?哪一个更好列出字典字典里面字典用于搜索密钥以检索值?

1 个答案:

答案 0 :(得分:6)

valseq = ['1', '2', '3', '4', '0', 'x1', 'x2']

newdata = [dict(zip(seq, valseq)) for seq in data]