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

2024-05-15 20:49:07 发布

您现在位置:Python中文网/ 问答频道 /正文

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

line_no_A= np.arange (17, 31)
sep_A=23

这些线以两个垂直方向排列,如蓝色和红色线所示。从sep开始,行的方向改变。 我还有创建这些线的点数:

point_rep_A=np.array([4, 3, 3, 1])

现在,我想提取一些特定的行。我上传了一张图,并用蓝色圆圈显示了细节线。为了在场景_A中找到带圆圈的红线,我可以首先检查point_rep_A:第一个值是4,第二个值是3,产生1个差异:因此,从第一条point_rep_A[0] - 1红线开始,保留第一条(行号24242526之外)。然后,point_rep_A[1] - point_rep_A[2]之间的差值为零,因此从point_rep_A[1] - 1红线开始,不保留任何一条。然后,point_rep_A[2] - point_rep_A[3]等于2,所以保留前两行(2930)。对于point_rep_A[4],如果它是1,则表示只有一个点,不能形成任何红线。但是,如果它大于一,我希望所有的行都不出现point_rep_A[4] - 1红线。 对于特定的böue行,point_rep_A[1] - point_rep_A[2]不是零,所以我想保留下一行的第一行。我指的是{}蓝线的第一行(即{}、{}和{}中的{})。第一行point_rep_A[2] - 1蓝线(即202122中的20)。最后是point_rep_A[3] - 1蓝线的第一行(这是23中的23)。事实上,我想在point_rep_A发生第一次更改之后开始选择这些行。我认为我的无花果可以更好地说明这个想法。最后我想得到这样的结果:

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

对于场景B,我的特定行是每个区块的最后一行。这里我想要每个区块的所有蓝线,但在前一个区块中,我想要在point_rep_A中的第一次更改之后开始拾取它们。我对方案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为我的想法设计一个算法,我将不胜感激。提前谢谢

enter image description here


Tags: nonpline场景区块方向arraysep
1条回答
网友
1楼 · 发布于 2024-05-15 20:49:07

这是第一部分:

# 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.]

相关问题 更多 >