拆分列表python

时间:2016-02-10 06:22:34

标签: python list

[' -80.82581786107986,39.83903198141125,0 -80.82377033116026,39.83364133601582,0 -80.82356083750963,39.82911201506083,0 -80.82285757569279,39.82686138006091,0']

我从3个多边形的KML文件中提取坐标,其中一个多边形的坐标存储在列表的一个元素中。我想分别计算经度和纬度的最小值。有谁知道这是怎么做到的吗?

我是否需要以某种方式拆分列表然后继续?

非常感谢!

3 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西。 注意:在Python 2.x

我取单个字符串,剥去外部空格,将值拆分为空格或逗号。这会产生这个数组,您可以在组中循环以获取值。

['-80.82581786107986', '39.83903198141125', '0', '-80.82377033116026', '39.83364133601582', '0', '-80.82356083750963', '39.82911201506083', '0', '-80.82285757569279', '39.82686138006091', '0']
import re

l = [' -80.82581786107986,39.83903198141125,0 -80.82377033116026,39.83364133601582,0 -80.82356083750963,39.82911201506083,0',
    '-80.82285757569279,39.82686138006091,0 -80.82211394716366,39.82370641582035,0 -80.82079041778377,39.82101855094219,0',
    ' -80.82008287730855,39.84462640578131,0 -80.82581786107986,39.83903198141125,0']

for s in l: 
    parts = map(float, re.split(r'[,\s+]', s.strip()))

    lats = []
    longs = []

    for i in range(0, len(parts), 3):
        long = parts[i]
        lat = parts[i+1]
        longs.append(long)
        lats.append(lat)

    print min(lats), min(longs)

<强>输出

39.8291120151 -80.8258178611
39.8210185509 -80.8228575757
39.8390319814 -80.8258178611

答案 1 :(得分:0)

你去:

>>> my_list = [' -80.82581786107986,39.83903198141125,0 -80.82377033116026,39.83364133601582,0 -80.82356083750963,39.82911201506083,0 -80.82285757569279,39.82686138006091,0']
>>> map(lambda x: map(float, x.split(",")), my_list[0].strip().split())
[[-80.82581786107986, 39.83903198141125, 0.0], [-80.82377033116026, 39.83364133601582, 0.0], [-80.82356083750963, 39.82911201506083, 0.0], [-80.8228575756928, 39.82686138006091, 0.0]]

纬度和经度的平均值:

>>> map(lambda x: (x[0]+x[1])/2, map(lambda x: map(float, x.split(",")), my_list[0].strip().split()))
[-20.4933929398343, -20.49506449757222, -20.4972244112244, -20.49799809781594]

你也可以使用熊猫:

>>> import `pandas` as pd
>>> pd.DataFrame(map(lambda x: map(float, x.split(",")), my_list[0].strip().split()))
           0          1  2
0 -80.825818  39.839032  0
1 -80.823770  39.833641  0
2 -80.823561  39.829112  0
3 -80.822858  39.826861  0
>>> data = pd.DataFrame(map(lambda x: map(float, x.split(",")), my_list[0].strip().split()))
>>> data.mean()
0   -80.824002
1    39.832162
2     0.000000
dtype: float64

答案 2 :(得分:0)

使用list comprehensionstr.split()str.strip方法:

l = [' -80.82581786107986,39.83903198141125,0 -80.82377033116026,39.83364133601582,0 -80.82356083750963,39.82911201506083,0 -80.82285757569279,39.82686138006091,0']
res = [list(map(float, coord.split(','))) for coord in l[0].strip().split()]

In [217]: res 
Out[217]:
[[-80.82581786107986, 39.83903198141125, 0.0],
 [-80.82377033116026, 39.83364133601582, 0.0],
 [-80.82356083750963, 39.82911201506083, 0.0],
 [-80.8228575756928, 39.82686138006091, 0.0]]

要计算最小值,经度和纬度,您可以将该列表转换为np.array以便于操作,并使用ndarray方法minaxis=0np.amin一起使用:

import numpy as np
arr = np.array(res)

In [221]: arr
Out[221]:
array([[-80.82581786,  39.83903198,   0.        ],
       [-80.82377033,  39.83364134,   0.        ],
       [-80.82356084,  39.82911202,   0.        ],
       [-80.82285758,  39.82686138,   0.        ]])

In [224]: arr.min(axis=0)
Out[224]: array([-80.82581786,  39.82686138,   0.        ])

In [226]: np.amin(arr, axis=0)
Out[226]: array([-80.82581786,  39.82686138,   0.        ])