简体   繁体   中英

Remove quotes from list - Python

I have the following string which contains coordinates of different poligons [x,y],[x,y]...

test3 = [(39.414604,-7.021836),(39.814045,-4.073939),(41.315235,-4.384754),(40.917038,-7.398971),(39.414604,-7.021836)],[(38.844185,-4.804621),(39.241299,-1.899833),(40.74308,-2.205491),(40.34742,-5.17429),(38.844185,-4.804621)],[(40.347511,-5.174313),(40.743309,-2.20434),(42.242691,-2.530139),(41.847603,-5.569263),(40.347511,-5.174313)],[(39.365192,-1.941078),(40.867912,-1.567062),(41.276688,-4.670904),(39.775406,-4.976737),(39.365192,-1.941078)],[(39.706161,-1.849863),(41.207623,-1.465817),(41.617561,-4.594476),(40.117233,-4.908839),(39.706161,-1.849863)],[(39.702591,-5.033657),(40.101254,-2.077048),(41.602196,-2.389729),(41.204681,-5.413605),(39.702591,-5.033657)],[(40.502308,-3.713448),(42.002708,-3.320609),(42.411217,-6.481188),(40.911678,-6.801756),(40.502308,-3.713448)],[(38.999126,-4.084739),(40.50222,-3.713471),(40.911449,-6.800602),(39.409878,-7.104931),(38.999126,-4.084739)],[(39.706161,-1.849863),(41.207623,-1.465817),(41.617561,-4.594476),(40.117233,-4.908839),(39.706161,-1.849863)]

When chekcing the output of test3 , it comes with ' and ' ' at the begining and end (this can be avoided by using print(test3)`

> test3
'[(39.414604,-7.021836),(39.814045,-4.073939),(41.315235,-4.384754),(40.917038,-7.398971),(39.414604,-7.021836)],[(38.844185,-4.804621),(39.241299,-1.899833),(40.74308,-2.205491),(40.34742,-5.17429),(38.844185,-4.804621)],[(40.347511,-5.174313),(40.743309,-2.20434),(42.242691,-2.530139),(41.847603,-5.569263),(40.347511,-5.174313)],[(39.365192,-1.941078),(40.867912,-1.567062),(41.276688,-4.670904),(39.775406,-4.976737),(39.365192,-1.941078)],[(39.706161,-1.849863),(41.207623,-1.465817),(41.617561,-4.594476),(40.117233,-4.908839),(39.706161,-1.849863)],[(39.702591,-5.033657),(40.101254,-2.077048),(41.602196,-2.389729),(41.204681,-5.413605),(39.702591,-5.033657)],[(40.502308,-3.713448),(42.002708,-3.320609),(42.411217,-6.481188),(40.911678,-6.801756),(40.502308,-3.713448)],[(38.999126,-4.084739),(40.50222,-3.713471),(40.911449,-6.800602),(39.409878,-7.104931),(38.999126,-4.084739)],[(39.706161,-1.849863),(41.207623,-1.465817),(41.617561,-4.594476),(40.117233,-4.908839),(39.706161,-1.849863)]'

I want to use this string as the coordinate information to plot the polygons in an interactive map. For this I use the following code, specifically the locations paramter that requires a list input

from ipyleaflet import Map, Polygon

polygon = Polygon(
    locations=[test3],
    color="green",
    fill_opacity= 0.5,
    fill_color="green"
)

m = Map(center=(38.5531, -4.6914), zoom=6)
m.add_layer(polygon);
m

The problem is that [test3] is equal to: (note the ['[ at the begining and the ]'] at the end

['[(39.414604,-7.021836),(39.814045,-4.073939),(41.315235,-4.384754),(40.917038,-7.398971),(39.414604,-7.021836)],[(38.844185,-4.804621),(39.241299,-1.899833),(40.74308,-2.205491),(40.34742,-5.17429),(38.844185,-4.804621)],[(40.347511,-5.174313),(40.743309,-2.20434),(42.242691,-2.530139),(41.847603,-5.569263),(40.347511,-5.174313)],[(39.365192,-1.941078),(40.867912,-1.567062),(41.276688,-4.670904),(39.775406,-4.976737),(39.365192,-1.941078)],[(39.706161,-1.849863),(41.207623,-1.465817),(41.617561,-4.594476),(40.117233,-4.908839),(39.706161,-1.849863)],[(39.702591,-5.033657),(40.101254,-2.077048),(41.602196,-2.389729),(41.204681,-5.413605),(39.702591,-5.033657)],[(40.502308,-3.713448),(42.002708,-3.320609),(42.411217,-6.481188),(40.911678,-6.801756),(40.502308,-3.713448)],[(38.999126,-4.084739),(40.50222,-3.713471),(40.911449,-6.800602),(39.409878,-7.104931),(38.999126,-4.084739)],[(39.706161,-1.849863),(41.207623,-1.465817),(41.617561,-4.594476),(40.117233,-4.908839),(39.706161,-1.849863)]']

My questions is: how to get rid of that ' at the begining and end of test3 so that when doing [test3] , the input is taken properly - as [[x,y],[x,y],...,[x,y]]

-- EDIT -- Where test3 is comming?

It starts with a dataframe :

df['Footprint']
0    MULTIPOLYGON (((-6.854965 38.740932,-3.940099 ...
1    MULTIPOLYGON (((-7.021836 39.414604,-4.073939 ...
2    MULTIPOLYGON (((-4.804621 38.844185,-1.899833 ...
3    MULTIPOLYGON (((-5.174313 40.347511,-2.20434 4...
4    MULTIPOLYGON (((-1.941078 39.365192,-1.567062 ...
5    MULTIPOLYGON (((-1.849863 39.706161,-1.465817 ...
6    MULTIPOLYGON (((-5.033657 39.702591,-2.077048 ...
7    MULTIPOLYGON (((-3.713448 40.502308,-3.320609 ...
8    MULTIPOLYGON (((-4.084739 38.999126,-3.713471 ...
9    MULTIPOLYGON (((-3.870231 39.827106,-3.49322 4...
Name: Footprint, dtype: object

Next, I invert the order of the coordinates from (x,y) to (y,x)

test2 =  df['Footprint'].map(lambda polygon: shapely.ops.transform(lambda x, y: (y, x), shapely.wkt.loads(polygon)))
test2

0    (POLYGON ((38.740932 -6.854965, 39.140369 -3.9...
1    (POLYGON ((39.414604 -7.021836, 39.814045 -4.0...
2    (POLYGON ((38.844185 -4.804621, 39.241299 -1.8...
3    (POLYGON ((40.347511 -5.174313, 40.743309 -2.2...
4    (POLYGON ((39.365192 -1.941078, 40.867912 -1.5...
5    (POLYGON ((39.706161 -1.849863, 41.207623 -1.4...
6    (POLYGON ((39.702591 -5.033657, 40.101254 -2.0...
7    (POLYGON ((40.502308 -3.713448, 42.002708 -3.3...
8    (POLYGON ((38.999126 -4.084739, 40.50222 -3.71...
9    (POLYGON ((39.827106 -3.870231, 41.329609 -3.4...
Name: Footprint, dtype: object

Next, I create a list of strings containing with the coordinates:

geom_str = []
for i in test2:  # same for multipolygon.geoms
    test = str(i)
    geom_str.append(test)
geom_str

['MULTIPOLYGON (((38.740932 -6.854965, 39.140369 -3.940099, 40.6422 -4.245644, 40.244202 -7.224669, 38.740932 -6.854965)))',
 'MULTIPOLYGON (((39.414604 -7.021836, 39.814045 -4.073939, 41.315235 -4.384754, 40.917038 -7.398971, 39.414604 -7.021836)))',
 'MULTIPOLYGON (((38.844185 -4.804621, 39.241299 -1.899833, 40.74308 -2.205491, 40.34742 -5.17429, 38.844185 -4.804621)))',
 'MULTIPOLYGON (((40.347511 -5.174313, 40.743309 -2.20434, 42.242691 -2.530139, 41.847603 -5.569263, 40.347511 -5.174313)))',
 'MULTIPOLYGON (((39.365192 -1.941078, 40.867912 -1.567062, 41.276688 -4.670904, 39.775406 -4.976737, 39.365192 -1.941078)))',
 'MULTIPOLYGON (((39.706161 -1.849863, 41.207623 -1.465817, 41.617561 -4.594476, 40.117233 -4.908839, 39.706161 -1.849863)))',
 'MULTIPOLYGON (((39.702591 -5.033657, 40.101254 -2.077048, 41.602196 -2.389729, 41.204681 -5.413605, 39.702591 -5.033657)))',
 'MULTIPOLYGON (((40.502308 -3.713448, 42.002708 -3.320609, 42.411217 -6.481188, 40.911678 -6.801756, 40.502308 -3.713448)))',
 'MULTIPOLYGON (((38.999126 -4.084739, 40.50222 -3.713471, 40.911449 -6.800602, 39.409878 -7.104931, 38.999126 -4.084739)))',
 'MULTIPOLYGON (((39.827106 -3.870231, 41.329609 -3.49322, 41.739006 -6.624273, 40.237854 -6.931492, 39.827106 -3.870231)))']

Next, I remove some of the unnecesary characters:

extent = []

for i in range(len(geom_str)):
    test = geom_str[i]
    test = test.replace('MULTIPOLYGON (((', '(')
    test = test.replace(')))', ')')
    test = test.replace(', ', ')')
    test = test.replace(')', '),(')
    test = test.replace(' ', ',')
    test = str(test)[:-2]
    extent.append(test)

extent
['(38.740932,-6.854965),(39.140369,-3.940099),(40.6422,-4.245644),(40.244202,-7.224669),(38.740932,-6.854965)',
 '(39.414604,-7.021836),(39.814045,-4.073939),(41.315235,-4.384754),(40.917038,-7.398971),(39.414604,-7.021836)',
 '(38.844185,-4.804621),(39.241299,-1.899833),(40.74308,-2.205491),(40.34742,-5.17429),(38.844185,-4.804621)',
 '(40.347511,-5.174313),(40.743309,-2.20434),(42.242691,-2.530139),(41.847603,-5.569263),(40.347511,-5.174313)',
 '(39.365192,-1.941078),(40.867912,-1.567062),(41.276688,-4.670904),(39.775406,-4.976737),(39.365192,-1.941078)',
 '(39.706161,-1.849863),(41.207623,-1.465817),(41.617561,-4.594476),(40.117233,-4.908839),(39.706161,-1.849863)',
 '(39.702591,-5.033657),(40.101254,-2.077048),(41.602196,-2.389729),(41.204681,-5.413605),(39.702591,-5.033657)',
 '(40.502308,-3.713448),(42.002708,-3.320609),(42.411217,-6.481188),(40.911678,-6.801756),(40.502308,-3.713448)',
 '(38.999126,-4.084739),(40.50222,-3.713471),(40.911449,-6.800602),(39.409878,-7.104931),(38.999126,-4.084739)',
 '(39.827106,-3.870231),(41.329609,-3.49322),(41.739006,-6.624273),(40.237854,-6.931492),(39.827106,-3.870231)']

Next, I add [] at the begining of each (x,y) coordinate:

extent2 = []

for i in range(len(extent)):
    test = '[' + extent[i] + '],'
    extent2.append(test)

Next, I remove some unncesary character at the very end:

extent2[len(extent2)-1] = str(extent2[5])[:-1]

Next, I concatenate all the indexes together (THIS STEP i WOULD LIKE TO TRANSFORM IT IN A FOR LOOP)

test = [extent2[1]+extent2[2]+extent2[3]+extent2[4]+extent2[5]+extent2[6]+extent2[7]+extent2[8]+extent2[9]]

Finally, I end up having test3 as follows:

test3 = str(test).replace("['[", '[')
test3 = str(test3).replace("]']", ']')
test3

I think it is a bad practice to manipulate data in string format:

If I take your example, instead of converting to a string, you should find out the type of "i" (in the following for loop) in order to manipulate it properly.

geom_str = []
for i in test2:  # same for multipolygon.geoms
    print(type(i), i) # <- What happens here ? Is i a list ? a tuple ?
    test = str(i) 
    geom_str.append(test)
geom_str

Can you try to see the type of this "i" in for loop, and the value you get if you print it. I will try to manipulate this data for you as soon as I know what it looks like.

This is API response data or serialised data of JSON.

Try below solution for your issue.

import json

json.loads(<pass your data over here>)

It will convert your data into the actual list or any dictionary object.

您可以使用 Python 内置的eval()将表示列表或元组的字符串计算为该对象。

test3 = eval(test3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM