Pandey Rashmi
Pandey Rashmi

Reputation: 157

how to make dictionary/list for x,y coordinate as a key and number/character as value?

I have such data structure which is shown below:

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

I need such structure:

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

Is there any piece of code which can help me sort out this structure or dynamically generate it? Which one is better list inside dictionary or dictionary inside dictionary for searching key to retrieve value?

Upvotes: 0

Views: 3155

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

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

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

Upvotes: 7

Related Questions