我有以下形式的 GeoJSON 输出:{'type': 'Point', 'coordinates': (0.00027777777777777827, 22.000138888888873)}如何将“坐标”的值从元组转换为列表,同时保持“类型”不变?我已经尝试使用{k: [list(ti) for ti in v] for k, v in geom.items()}给出这里那里geom = geometry.mapping(geometry.Point(x, y)),但它并不能帮助。我看到一条错误消息,指出“浮动对象不可迭代”我正在使用匀称的库
3 回答
芜湖不芜
TA贡献1796条经验 获得超7个赞
您只能将'coordinates'
密钥更改为列表:
geom['coordinates'] = list(geom['coordinates'])
输出:
{'type': 'Point', 'coordinates': [0.00027777777777777827, 22.000138888888873]}
慕勒3428872
TA贡献1848条经验 获得超6个赞
非常简单的方法,通过 Python 控制台进行测试:
>>> d = {'type': 'Point', 'coordinates': (0.00027777777777777827, 22.000138888888873)}
>>>
>>> type(d)
<class 'dict'>
>>>
>>> d['coordinates']
(0.00027777777777777827, 22.000138888888873)
>>>
>>> type(d['coordinates'])
<class 'tuple'>
>>>
>>> list(d['coordinates'])
[0.00027777777777777827, 22.000138888888873]
最后,你只覆盖字典的内容:
>>> d['coordinates'] = list(d['coordinates'])
添加回答
举报
0/150
提交
取消