如何根据python中的条件提取特定的数字

如何根据python中的条件提取特定的数字,python,algorithm,numpy,Python,Algorithm,Numpy,通过连接一些点,我创建了一组线。我将行数设置为numpy数组: line_no_A= np.arange (17, 31) sep_A=23 这些线以两个垂直方向排列,如蓝色和红色线所示。从sep,行的方向改变。 我还有创建这些线的点数: point_rep_A=np.array([4, 3, 3, 1]) 现在,我想提取一些特定的行。我上传了一张图,并用蓝色圆圈显示了细节线。要在场景A中找到带圆圈的红线,我可以首先检查点A:第一个值是4,第二个值是3,产生1个差异:因此,从第一个点A[0]

通过连接一些点,我创建了一组线。我将行数设置为numpy数组:

line_no_A= np.arange (17, 31)
sep_A=23
这些线以两个垂直方向排列,如蓝色和红色线所示。从
sep
,行的方向改变。 我还有创建这些线的点数:

point_rep_A=np.array([4, 3, 3, 1])
现在,我想提取一些特定的行。我上传了一张图,并用蓝色圆圈显示了细节线。要在场景A中找到带圆圈的红线,我可以首先检查
点A
:第一个值是4,第二个值是3,产生1个差异:因此,从第一个
点A[0]-1
红线开始,保留第一个(行号
24
不在
24
25
26
)。然后,
point\u rep\u A[1]-point\u rep\u A[2]
之间的差值为零,因此从
point\u rep\u A[1]-1
红线开始,不保留它们。然后,
point\u rep\u A[2]-point\u rep\u A[3]
等于2,所以保持前两行(
29
30
)。对于
点_rep_A[4]
,如果它是1,则表示只有一个点,不能形成任何红线。但是,如果它比一大,我想让所有的线都远离
点[4]-1
红线。 对于特定的böue行,
point_rep_A[1]-point_rep_A[2]
不是零,所以我想保留下一行的第一行。我是指
点的第一行\u rep_A[1]-1
蓝线(这是
17
中的
17
18
19
)。
点的第一行代表[2]-1
蓝线(即
20
21
22
中的
20
)。最后是
点A[3]-1
蓝线的第一行(这是
23
中的
23
)。事实上,我想在
point\u rep\u A
发生第一次更改后开始拾取这些行。我认为我的无花果可以更好地说明这个想法。最后我想得到这样的结果:

[24, 17, 20, 29, 30, 23]
对于场景B,我的特定行是每个区块的最后一行。这里我想要每个区块的所有蓝线,但在前一个区块中,我想要在
点的第一次更改后开始拾取它们。我对方案B的输入是:

line_no_B= np.arange (17, 34)
sep_B=25
point_rep_B=np.array([1, 2, 3, 3, 4])
我希望场景B有这样的结果:

[17, 26, 19, 28, 22, 25, 33]
如果有人能帮我用python为我的想法设计一个算法,我将不胜感激。提前谢谢


这是第一部分:

# SCENARIO A

import numpy as np
from scipy.ndimage.interpolation import shift

# Input data
line_no_A = np.arange(17, 31)
sep_A = 23
point_rep_A = np.array([4, 3, 3, 1])

# RED LINES

# Red candidates
point_rep_red = point_rep_A - 1
red_A = line_no_A[np.where(line_no_A == sep_A)[0][0]+1:]

# Build red array
red_array = np.zeros(shape=(3, 4))
red_lines_index = np.cumsum(point_rep_red)
red_lines_index = np.insert(red_lines_index, 0, 0)
for i in range(len(red_lines_index)-1):
    red_array[:,i][:point_rep_red[i]] = red_A[red_lines_index[i]:red_lines_index[i + 1]][::-1]

# Select red lines
shift_rep_A = point_rep_red - shift(point_rep_red, -1, cval=0)
red_specific = [red_array[:, i][:point_rep_red[i]][red_array[:, i][:point_rep_red[i]].size-ind:][::-1] for i, ind in enumerate(shift_rep_A)]
red_specific = np.array(red_specific, dtype=object)

# BLUE LINES

# Blue candidates
blue_A = line_no_A[:np.where(line_no_A == sep_A)[0][0]+1]

# Build blue array
blue_array = np.zeros(shape=(3, 3))
blue_lines_index = np.cumsum(point_rep_A[1:])
blue_lines_index = np.insert(blue_lines_index, 0, 0)
for i in range(len(blue_lines_index)-1):
    blue_array[:,i][:point_rep_A[i+1]] = blue_A[blue_lines_index[i]:blue_lines_index[i + 1]][::-1]

# Select blue lines
first_change_index = np.argmax(shift_rep_A > 0)  # index of first change in point_rep_A
blue_specific = np.array([blue_array[point_rep_A[i+1]-1, i] for i in range(blue_array.shape[1])])
blue_specific[:first_change_index] = np.nan  # skip values before the first change in point_rep_A
blue_specific = np.append(blue_specific, np.nan)  # make it the same length as red_specific
blue_specific = np.array(blue_specific, dtype=object)

# Merge red lines and blue lines
result_A = np.hstack(np.ravel(np.column_stack((red_specific, blue_specific))))
result_A = result_A[~np.isnan(result_A)]  # remove NaNs
print(result_A)
输出为:

[24. 17. 20. 29. 30. 23.]

“对于特定的蓝线,
点代表A[1]-点代表A[2]
不是零”。好的,根据你自己对点的定义,
点[1]-点[2]
是零。亲爱的@DavidM.,对于蓝线,我想找出
点的值在哪里变化:它在第二个值中变化(第一个值是
4
,第二个值是
3
)。因此,我想选择以下块的第一个值:
17
中的第一个、
18
19
20
中的第一个、
21
22
中的第一个、
23
中的第一个。例如,如果更改发生在
点A
的第三个值中,那么我只想选择
20
21
22
的第一个值,以及
23
的第一个值。事实上,我想在
point_rep_A
中第一次更改后开始拾取第一个值。所以你的意思是
point_rep_A[0]-point_rep_A[1]
不是零?亲爱的@David M,确切地说,我想开始在更改后拾取蓝线的过程。为了将其可视化,让我们考虑一下场景A中没有编号为
24
的红线。然后,我不需要编号为
17
20
的蓝线,我只需要编号为
23
的蓝线。我已经解决了方案A。请告知此解决方案是否满足您的目标。