如何根据多个分隔符拆分字符串

时间:2021-05-26 07:33:35

标签: python string split

我有以下数据,它是数组 featuresCollectionGeometries 中索引为零的元素编号 1,因此 featuresCollectionGeometries[0] 返回以下数据 `

    ['6.69223420722764 51.1329165124371,6.69222819326484 51.1329081193748,6.69221180776184 51.1329013203209,6.69219149698336 51.1329073049609,6.69217949340067 51.1329244260894,6.69217956546811 51.1329430447612,6.69218526127669 51.1329578171713,6.692200130301 51.1329606289999,6.69220511560768 51.1329581192089,6.69221372090576 51.1329518135339,6.69222707128915 51.132939215911,6.69223420722764 51.1329165124371']

所以数组中的每个条目或元素都是一个具有上述格式的字符串。 我想要完成的是将经度值分开并将它们添加到“longitudeValuesArray”中并将纬度值分开并将它们添加到“latitudeValuesArray” 注意

    the entries separated by comma represents longitude and latitude. for example, given the above mentioned values
    `longitudeValuesArray should contain['6.69223420722764,6.69222819326484,6.69221180776184,6.69219149698336,6.69217949340067,6.69217956546811,6.69218526127669,6.692200130301,6.69220511560768,6.69221372090576,6.69222707128915,6.69223420722764']
    `latitudeValuesArray should contain['51.1329165124371,51.1329081193748,51.1329013203209,51.1329073049609,51.1329244260894,51.1329430447612,51.1329578171713,51.1329606289999,51.1329581192089,51.1329518135339,51.132939215911,51.1329165124371']

请让我知道如何拆分包含经度和纬度值的字符串以实现我的目标

5 个答案:

答案 0 :(得分:1)

您可以使用内置模块 for(j = 0; j<i+1; ++j{ 中的 re.split 来拆分多个分隔符

re

其中import re data = '6.69223420722764 51.1329165124371,6.69222819326484 51.1329081193748,6.69221180776184 51.1329013203209,6.69219149698336 51.1329073049609,6.69217949340067 51.1329244260894,6.69217956546811 51.1329430447612,6.69218526127669 51.1329578171713,6.692200130301 51.1329606289999,6.69220511560768 51.1329581192089,6.69221372090576 51.1329518135339,6.69222707128915 51.132939215911,6.69223420722764 51.1329165124371' parts = re.split(' |,', data) 表示,然后使用切片提取经纬度

|

输出

lons = parts[::2]
lats = parts[1::2]
print(lons)
print(lats)

答案 1 :(得分:0)

我假设您的 featuresCollectionGeometries 有多个值,每个值都包含您显示的行。在这种情况下,循环遍历 featuresCollectionGeometries 中的元素,拆分逗号上的数据,然后循环遍历经度和纬度对。拆分它们并将它们附加到自己的列表中。

longitude_values = []
latitude_values = []

for i in featuresCollectionGeometries:
    data = i.split(',')
    for longlat in data:
        longitude_values.append(longlat.split(' ')[0])
        latitude_values.append(longlat.split(' ')[1])
    

答案 2 :(得分:0)

您可以在整个字符串上使用 split(','),然后在纬度和经度的每个条目上使用 split()

lst =  ['6.69223420722764 51.1329165124371,6.69222819326484 51.1329081193748,6.69221180776184 51.1329013203209,6.69219149698336 51.1329073049609,6.69217949340067 51.1329244260894,6.69217956546811 51.1329430447612,6.69218526127669 51.1329578171713,6.692200130301 51.1329606289999,6.69220511560768 51.1329581192089,6.69221372090576 51.1329518135339,6.69222707128915 51.132939215911,6.69223420722764 51.1329165124371']

res = [float(y) for x in lst[0].split(',') for y in x.split()]
lng, lat = res[::2], res[1::2]

print(lng)
#[6.69223420722764, 6.69222819326484, 6.69221180776184, 6.69219149698336, 6.69217949340067, 6.69217956546811, 6.69218526127669, 6.692200130301, 6.69220511560768, 6.69221372090576, 6.69222707128915, 6.69223420722764]
print(lat)
#[51.1329165124371, 51.1329081193748, 51.1329013203209, 51.1329073049609, 51.1329244260894, 51.1329430447612, 51.1329578171713, 51.1329606289999, 51.1329581192089, 51.1329518135339, 51.132939215911, 51.1329165124371]

答案 3 :(得分:0)

这是一种方法:

orig_list = ['6.69223420722764 51.1329165124371,6.69222819326484 51.1329081193748,6.69221180776184 51.1329013203209,6.69219149698336 51.1329073049609,6.69217949340067 51.1329244260894,6.69217956546811 51.1329430447612,6.69218526127669 51.1329578171713,6.692200130301 51.1329606289999,6.69220511560768 51.1329581192089,6.69221372090576 51.1329518135339,6.69222707128915 51.132939215911,6.69223420722764 51.1329165124371']

split_list = orig_list[0].split(',')

long_list = [value.split()[0] for value in split_list]
lat_list  = [value.split()[1] for value in split_list]

答案 4 :(得分:0)

main_list = featuresCollectionGeometries[0].split(',')

longitudeValuesArray = [float(elem_lat.split()[0]) for elem_lat in main_list]

latitudeValuesArray = [float(elem_lat.split()[1]) for elem_lat in main_list]