我有以下形式的 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
提交
取消