将元组转换为整数 - python

Convert tuple to int - python

我有以下 table ( df):

形状 数据
多边形 ((1280 16068.18, 1294 16059, 1297 16060, 1300 16063, 1303 16065, 1308 16066))
点 ((37916311947 12769))
多边形 多边形 ((1906.23 12983, 1908 12982, 1916 12974, 1917 12972, 1917 12970))

我想将 table 转换为以下格式:

期望的输出:

converted_data
[(1280, 16068), (1294, 16059), (1297, 16060), (1300, 16063), (1303, 16065), (1308, 16066)]
[(37916311947, 12769)]
[(1906, 12983), (1908, 12982), (1916, 12974), (1917, 12972), (1917, 12970)]

我想修改括号并添加逗号并删除单词 POLYGON 或 POINT。到目前为止我尝试了什么?

res1 = []
for ip, geom in zip(df2['data'], df2['SHAPE']):
    if geom == 'POINT':
        st = str(ip)[8:-2]
    elif geom == 'POLYGON/SURFACE':
        st = str(ip)[10:-2]
    s = st.split(',')
    res1.append(s)

res = []
for i in res1:
    res.append([tuple(map(int, j.split())) for j in i])

data2 = df2.copy()
data2['converted_data']=res
´´´

The above script works saves the output as tuple and not int. How do I optimize my script?

您的代码的第一部分看起来不错 - 在第二部分中,您可能试图拆分 i 而不是 j

x = '1280 16068.18, 1294 16059, 1297 16060, 1300 16063, 1303 16065, 1308 16066'
x_split = [tuple(map(lambda x: int(float(x)), i.strip().split())) for i in x.strip().split(',')] 
#[(1280, 16068),
# (1294, 16059),
# (1297, 16060),
# (1300, 16063),
# (1303, 16065),
# (1308, 16066)]
df = pd.DataFrame([['POLYGON', '((1280 16068.18, 1294 16059, 1297 16060, 1300 16063, 1303 16065, 1308 16066))'],
                  ['POINT', 'POINT ((37916311947 12769))'],
                  ['POLYGON', 'POLYGON ((1906.23 12983, 1908 12982, 1916 12974, 1917 12972, 1917 12970))']], columns=['shape', 'data'])

df['data'] = df['data'].str.findall(r'(\d[\d.\s]+\d)').apply(lambda x: [tuple(map(lambda x: int(float(x)), i.split())) for i in x])

df
    shape   data
0   POLYGON [(1280, 16068), (1294, 16059), (1297, 16060), ...
1   POINT   [(37916311947, 12769)]
2   POLYGON [(1906, 12983), (1908, 12982), (1916, 12974), ...