python 拆分和调用数据

hgqdbh6s  于 2023-09-29  发布在  Python
关注(0)|答案(2)|浏览(59)

I have data from a series of points that over lap between two graphs that give the following data: MULTIPOINT (2.143370447157314 0.1353352832366127, 75.99346394751342 0.1353352832366127, 94.39258581473268 0.1353352832366127, 111.8928901992089 0.1353352832366127, 127.0468781718633 0.1353352832366127, 184.5804306294127 0.1353352832366127, 204.2456523252587 0.1353352832366127, 219.5685478372618 0.1353352832366127, 231.2004389877476 0.1353352832366127, 293.5451724106475 0.1353352832366127, 310.9486085910552 0.1353352832366127, 330.0610044903921 0.1353352832366127, 338.7762476890871 0.1353352832366127, 402.2859828353771 0.1353352832366127, 422.3815761349065 0.1353352832366127, 440.9842862789425 0.1353352832366127, 451.6324097852983 0.1353352832366127, 512.7034613293686 0.1353352832366127, 530.623914129864 0.1353352832366127, 549.842226961968 0.1353352832366127, 561.6760004904758 0.1353352832366127, 624.6195484846207 0.1353352832366127, 640.701617729866 0.1353352832366127, 659.3176260680895 0.1353352832366127, 659.6827799404297 0.1353352832366127, 661.1778885763671 0.1353352832366127, 671.2977053828017 0.1353352832366127, 738.9468397381991 0.1353352832366127, 747.9185452191947 0.1353352832366127, 768.0657707819746 0.1353352832366127, 782.5204038757331 0.1353352832366127, 879.0203459813714 0.1353352832366127, 895.3297620941928 0.1353352832366127, 958.2812971302388 0.1353352832366127, 958.9412279666292 0.1353352832366127, 986.2760322593815 0.1353352832366127, 1001.912171992246 0.1353352832366127, 1096.858061280496 0.1353352832366127, 1118.376788346299 0.1353352832366127, 1203.92639341071 0.1353352832366127, 1220.384947439686 0.1353352832366127, 1317.401330046149 0.1353352832366127, 1317.502930130382 0.1353352832366127, 1318.858929927066 0.1353352832366127, 1319.772755799937 0.1353352832366127, 1323.478519672568 0.1353352832366127, 1325.491001741218 0.1353352832366127, 1429.099147413253 0.1353352832366127)
All of the Y values are the same and so I want to separate the X and Y values and only call the X values. I have tried using the .split function but it doesn't work. I also will have to do this with more data sets so if there's a universal way of doing it without knowing the X values that would be very helpful. Any help would be Greatly appreciated! I am using python.
here is my code:

df = pd.read_csv("C:\\Users\\Kyle\\Desktop\\Plot_Values1.csv")

Distance = df['distance']
intensity = df['Gray_Value']
intensity_norm = (intensity - intensity.min())/ (intensity.max() - intensity.min())

  
x = Distance
y = intensity_norm

y2= np.full_like(intensity_norm, 1 / math.exp(2))

plt.plot(x, intensity_norm)
plt.axhline(y=1/math.exp(2), color='r', linestyle='-')

first_line = LineString(np.column_stack((x, y2)))
second_line = LineString(np.column_stack((x, y)))
intersection = first_line.intersection(second_line)

Inter = np.full_like(intersection, intersection)

.split(" ") to try to split by spaces.

j2cgzkjk

j2cgzkjk1#

假设你正在使用Python。

str = "MULTIPOINT (2.143370447157314 0.1353352832366127, 75.99346394751342 0.1353352832366127, 94.39258581473268 0.1353352832366127, 111.8928901992089 0.1353352832366127, 127.0468781718633 0.1353352832366127, 184.5804306294127 0.1353352832366127, 204.2456523252587 0.1353352832366127, 219.5685478372618 0.1353352832366127, 231.2004389877476 0.1353352832366127, 293.5451724106475 0.1353352832366127, 310.9486085910552 0.1353352832366127, 330.0610044903921 0.1353352832366127, 338.7762476890871 0.1353352832366127, 402.2859828353771 0.1353352832366127, 422.3815761349065 0.1353352832366127, 440.9842862789425 0.1353352832366127, 451.6324097852983 0.1353352832366127, 512.7034613293686 0.1353352832366127, 530.623914129864 0.1353352832366127, 549.842226961968 0.1353352832366127, 561.6760004904758 0.1353352832366127, 624.6195484846207 0.1353352832366127, 640.701617729866 0.1353352832366127, 659.3176260680895 0.1353352832366127, 659.6827799404297 0.1353352832366127, 661.1778885763671 0.1353352832366127, 671.2977053828017 0.1353352832366127, 738.9468397381991 0.1353352832366127, 747.9185452191947 0.1353352832366127, 768.0657707819746 0.1353352832366127, 782.5204038757331 0.1353352832366127, 879.0203459813714 0.1353352832366127, 895.3297620941928 0.1353352832366127, 958.2812971302388 0.1353352832366127, 958.9412279666292 0.1353352832366127, 986.2760322593815 0.1353352832366127, 1001.912171992246 0.1353352832366127, 1096.858061280496 0.1353352832366127, 1118.376788346299 0.1353352832366127, 1203.92639341071 0.1353352832366127, 1220.384947439686 0.1353352832366127, 1317.401330046149 0.1353352832366127, 1317.502930130382 0.1353352832366127, 1318.858929927066 0.1353352832366127, 1319.772755799937 0.1353352832366127, 1323.478519672568 0.1353352832366127, 1325.491001741218 0.1353352832366127, 1429.099147413253 0.1353352832366127)"
left = str.find('(')
right = str.rfind(')')
x_list = [x.split(' ')[0] for x in str[left+1:right].split(', ')]

测试结果:

['2.143370447157314', '75.99346394751342', '94.39258581473268', '111.8928901992089', '127.0468781718633', '184.5804306294127', '204.2456523252587', '219.5685478372618', '231.2004389877476', '293.5451724106475', '310.9486085910552', '330.0610044903921', '338.7762476890871', '402.2859828353771', '422.3815761349065', '440.9842862789425', '451.6324097852983', '512.7034613293686', '530.623914129864', '549.842226961968', '561.6760004904758', '624.6195484846207', '640.701617729866', '659.3176260680895', '659.6827799404297', '661.1778885763671', '671.2977053828017', '738.9468397381991', '747.9185452191947', '768.0657707819746', '782.5204038757331', '879.0203459813714', '895.3297620941928', '958.2812971302388', '958.9412279666292', '986.2760322593815', '1001.912171992246', '1096.858061280496', '1118.376788346299', '1203.92639341071', '1220.384947439686', '1317.401330046149', '1317.502930130382', '1318.858929927066', '1319.772755799937', '1323.478519672568', '1325.491001741218', '1429.099147413253']

**编辑:**因为你看起来是在使用shapely,你可以遍历MultiPoint对象的几何集合,直接提取x的值。

范例:

from shapely import MultiPoint
mp_obj = MultiPoint([[2.143370447157314, 0.1353352832366127], 
                     [75.99346394751342, 0.1353352832366127],
                     [94.39258581473268, 0.1353352832366127]])
xs = [p.x for p in mp_obj.geoms]

测试结果:

[2.143370447157314, 75.99346394751342, 94.39258581473268]
pgky5nke

pgky5nke2#

  • "...所有的Y值都是相同的,所以我想把X和Y值分开,只调用X值。我试过使用.split函数,但它不起作用。我还必须用更多的数据集来做这件事,所以如果有一种通用的方法,在不知道X值的情况下,这将是非常有帮助的。..."*

有几种方法可以做到这一点。
这里有一种方法,使用 str.splitlist comprehension

y = [x[:x.find(' ')] for x in s.split(', ')]

您也可以使用 re 模块,并匹配这些值。

y = [x.group() for x in re.finditer('[^, ]+(?= )', s)]

或者,您可以使用 enumerate 函数来遍历 string

y = []
o = 0
for i, c in enumerate(s):
    if c == ' ':
        if s[i - 1] != ',': y.append(s[o:i])
        o = i + 1

输出,截断。

2.143370447157314
75.99346394751342
94.39258581473268
...
1323.478519672568
1325.491001741218
1429.099147413253

相关问题