[英]Convert a tuple to a list in a dictionary
I have a GeoJSON output in the form of: 我有以下形式的GeoJSON输出:
{'type': 'Point', 'coordinates': (0.00027777777777777827, 22.000138888888873)}
How can I convert the value of 'coordinates' from a tuple to a list while keeping 'type' as is? 如何在保持“类型”不变的情况下将“坐标”的值从元组转换为列表?
I've tried using {k: [list(ti) for ti in v] for k, v in geom.items()}
as given here where geom = geometry.mapping(geometry.Point(x, y))
but it doesn't help. 我已经尝试使用{k: [list(ti) for ti in v] for k, v in geom.items()}
给出这里其中geom = geometry.mapping(geometry.Point(x, y))
但它没有帮助。
I see an error that says 'Float object is not iterable' 我看到一条错误消息:“浮动对象不可迭代”
I'm using the shapely library 我正在使用匀称的图书馆
You can only change the 'coordinates'
key as a list: 您只能将'coordinates'
键更改为列表:
geom['coordinates'] = list(geom['coordinates'])
OUTPUT : 输出 :
{'type': 'Point', 'coordinates': [0.00027777777777777827, 22.000138888888873]}
You can convert directly as follows: 您可以直接进行如下转换:
a['coordinates'] = list(a['coordinates'])
where a
is your dict. 其中a
是你的字典。
Very simple way, testing it via Python console: 很简单的方法,通过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]
Finally, you only overwrite the contents of the dictionary: 最后,您只覆盖字典的内容:
>>> d['coordinates'] = list(d['coordinates'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.