将字典中的坐标指定为x和y

时间:2017-11-17 19:26:56

标签: python dictionary

我有一个文件shp_input,它是一个字典。

print shp_input[0]

输出:

{'geometry': {'type': 'Polygon', 'coordinates': [[(497072.2, 198376.46, 0.0), (497079.95, 198385.9, 0.0), (497089.45, 198378.1, 0.0), (497081.7, 198368.66, 0.0), (497072.2, 198376.46, 0.0)]]}, 'type': 'Feature', 'id': '0', 'properties': OrderedDict([(u'ID', u'5981CFD7-C9EA-4FC3-9C47-CF83B96CC672'), (u'FEATCODE', 15014), (u'SQM', 150.13)])}

访问坐标让我:

for item in shp_input:
            print [item][0]['geometry']['coordinates']

输出:

[[(487510.47000000003, 197000.35, 0.0), (487513.69, 197004.76, 0.0), 
(487518.79000000004, 197001.04, 0.0), (487515.57, 196996.63, 0.0), 
(487510.47000000003, 197000.35, 0.0)]]
[[(486284.7, 196638.62, 0.0), (486266.23, 196628.43, 0.0), (486261.56, 
196636.77000000002, 0.0), (486280.12, 196647.0, 0.0), (486284.7, 196638.62, 
0.0)]]
[[(486261.62, 196730.39, 0.0), (486270.08, 196731.51, 0.0), (486273.13, 
196708.35, 0.0), (486264.67, 196707.24, 0.0), (486261.62, 196730.39, 0.0)]]
[[(486283.77, 196801.27000000002, 0.0), (486271.05, 196798.64, 0.0), 
(486269.06, 196810.73, 0.0), (486281.87, 196812.84, 0.0), 
(486282.54000000004, 196808.77000000002, 0.0), (486284.87, 196812.87, 0.0), 
(486298.95, 196804.85, 0.0), (486293.9, 196795.98, 0.0), (486283.77, 
196801.27000000002, 0.0)]]

等等。我试图将每个x坐标分配给一个x变量,将每个y分配给一个y变量。我该怎么做?围绕每组坐标的双方括号也让我失望。

1 个答案:

答案 0 :(得分:1)

您可以使用简单的下标删除括号,例如:

coords

现在,您遍历for triple in coords: x, y, z = triple 列表,这应该会在每次迭代时给出三倍。只需从三元组中分配:

z

现在,做任何你需要做的事情,忽略_值。将apply用于你将要忽视的事情更具Pythonic。