I am new to python so I am trying to figure out how to use a generator expression with tuples of integers.
I have the following construct:
Number of Polygons: 3
Blocked Polygons:
{
0:(6, 192, 365, 172, 388, 115, 378, 127, 311, 142, 305, 192, 334),
1:(4, 172, 688, 115, 678, 105, 650, 107, 634),
2:(6, 242, 294, 215, 278, 205, 250, 242, 205, 284, 221, 292, 234)
}
Where Blocked Polygons is a dictionary object
the first item 6, 192, 365...etc.. where 6 is the number of coordinates in that list.
What I want to convert it to is a dictionary of coordinate dictionaries that looks like this:
{
0:{
0:(192, 365),
1:(172, 388),
2:(115, 378),
3:(127, 311),
4:(142, 305),
5:(192, 334)
},
}, etc...
any ideas on how to do this effectively?
Here is one way, example shown for the first dictionary key:
data = {
0: (6, 192, 365, 172, 388, 115, 378, 127, 311, 142, 305, 192, 334),
1: (4, 172, 688, 115, 678, 105, 650, 107, 634),
2: (6, 242, 294, 215, 278, 205, 250, 242, 205, 284, 221, 292, 234)
}
it = iter(data[0][1:])
result = dict(enumerate(zip(it, it)))
from pprint import pprint
pprint(result)
Output
{0: (192, 365),
1: (172, 388),
2: (115, 378),
3: (127, 311),
4: (142, 305),
5: (192, 334)}
You can do all keys in data like this:
results = {}
for k, v in data.items():
it = iter(v[1:])
results.update({k: dict(enumerate(zip(it, it)))})
pprint(results)
Output
{0: {0: (192, 365),
1: (172, 388),
2: (115, 378),
3: (127, 311),
4: (142, 305),
5: (192, 334)},
1: {0: (172, 688), 1: (115, 678), 2: (105, 650), 3: (107, 634)},
2: {0: (242, 294),
1: (215, 278),
2: (205, 250),
3: (242, 205),
4: (284, 221),
5: (292, 234)}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With