简体   繁体   English

如何提取 numpy 数组的某些特定行

[英]How to extract some specific rows of a numpy array

I have a unmpy array and want to extract some rows of the array.我有一个 unmpy 数组,想提取数组的一些行。 My array has three columns which are x, y and z coordinates:我的数组有三列,它们是 x、y 和 z 坐标:

sr_y=np.array([[10., 1., 8.], [20., 1., 8.1], [30., 1., 7.9], [10., 2., 8.],\
              [20., 2., 7.9], [30., 2., 8.], [10., 1., 1.9], [20., 1., 2.],\
              [10., 2., 2.1], [20., 2., 2.2], [30., 2., 1.8]])

The array is sorted one.数组排序为一。 The rows with the same values in their second column are such kind of subset.第二列中具有相同值的行就是这种子集。 First three rows of sr_y have the same value in their second column, so I want the last row ( [30., 1., 7.9] ). sr_y的前三行在它们的第二列中具有相同的值,所以我想要最后一行( [30., 1., 7.9] )。 Then, again rows 4, 5 and 6 make the next subset and I want the last row (the 6th).然后,第 4、5 和 6 行再次构成下一个子集,我想要最后一行(第 6 行)。 Then, from row 6 to 7, the third column changes a lot (from 8 to 1.9 ).然后,从第 6 行到第 7 行,第三列变化很大(从81.9 )。 From here to end, I want to extract the first row of each subset.从这里到结束,我想提取每个子集的第一行。 Rows 7 and 8 are a subset and I want row 7. From rows 9, 10 and 11, I want the first one (9th row).第 7 行和第 8 行是一个子集,我想要第 7 行。从第 9、10 和 11 行,我想要第一个(第 9 行)。 Briefly speaking, I want to following array:简而言之,我想关注数组:

np.array([[30., 1., 7.9], [30., 2., 8.], [10., 1., 1.9], [10., 2., 2.1]])

I tried the following code but it did not give me what I want:我尝试了以下代码,但它没有给我想要的东西:

exported_data=np.array([])
for i in range (len(sr_y)-1):
    if sr_y[i,1] != sr_y[i+1,1] or sr_y[i-1,2]>int (sr_y[i,2]+4):
        aa=sr_y[i]
        exported_data=np.append (exported_data,aa)
exported_data=exported_data.reshape(-1,3)

In fact, it can not modify role of export after the big change of the third column.事实上,在第三列大改之后,就不能修改export的角色了。 After that big change, I want to export the first row of each subset.在那次大的改变之后,我想导出每个子集的第一行。 In advance, Thanks for any help.在此先感谢您的帮助。 I do appreciate any contribution.我很感激任何贡献。

You can use a flag to indicate if the big step has already occured or not.您可以使用标志来指示大步骤是否已经发生。

delta = 4
before = True
for i in range(sr_y.shape[0]-1):
    if before and sr_y[i][0] > sr_y[i+1][0]:
        print('(last)', sr_y[i])
    if sr_y[i][2] > sr_y[i+1][2] + delta:
        before = False
    if not before and sr_y[i][0] > sr_y[i+1][0]:
        print('(first)', sr_y[i+1])

The first condition will catch last values ( > check) for all subsets before the big step ( before ).第一个条件将捕获大步骤before )之前所有子集的最后一个值( >检查)。 The second condition handles the flag switch.第二个条件处理标志开关。 While the third and last condition catches first values ( > check) for all subsets after the big step ( not before ).而第三个也是最后一个条件在大步骤之后( not before )捕获所有子集的第一个值( >检查)。

Note: Replace the print() calls with the list.append() accordingly.注意:相应地用list.append()替换print()调用。 It's important that the conditions remain in this order.保持此顺序的条件很重要。 Since on the iteration where the big set occurs, it will have to catch both the last value for the previous subset and the first value from the following.由于在发生大集合的迭代中,它必须同时捕获前一个子集的最后一个值和下一个子集的第一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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