简体   繁体   English

以并行方式从数组中删除条目

[英]Removing entries from arrays in a parallel manner

I have a list/array of x and y coordinates, for example: 我有一个x和y坐标的列表/数组,例如:

x = [x1, x2, x3,...]
y = [y1, y2, y3,...]

Now, I want to remove certain entries based on conditions, for example, the following: 现在,我想根据条件删除某些条目,例如,以下内容:

for i in x:
    if i <= 40 and i >= -40:
        print "True"
    else:
        x.remove(i)

for i in y:
    if i <= 20 and i >=- 20:
        print "True"
    else:
        y.remove(i)

The code above removes the respective entries from the lists, but if x1 is removed, y1 still remains in the list. 上面的代码从列表中删除了相应的条目,但如果删除x1y1仍然保留在列表中。 What I want to achieve is, if x1 is removed, y1 should also be removed. 我想要实现的是,如果删除x1 ,也应该删除y1 How can I go about doing this? 我该怎么做呢? My final goal is to try to plot x and y , so I am unable to do this as the lists end up having different dimensions. 我的最终目标是尝试绘制xy ,因此我无法做到这一点,因为列表最终会有不同的维度。 I can also use 我也可以用

zeta_list = np.column_stack((x, y))

to get an array like ([[x1, y1], [x2, y2], [x3, y3],...]]) , but I am not sure how to remove entries from this using an if conditional. 得到一个像([[x1, y1], [x2, y2], [x3, y3],...]])这样的数组,但我不知道如何使用if条件从中删除条目。

Thanks. 谢谢。

Form a boolean selection mask: 形成一个布尔选择掩码:

mask = ~((x > 40) | (x < -40) | (y > 20) | (y < -20))

then, to select values from x and y where mask is True: 然后,从mask为True的xy中选择值:

x, y = x[mask], y[mask]

When x is a NumPy array, (x > 40) returns a boolean array of the same shape as x which is True where the elements of x are greater than 40. x是一个NumPy的阵列, (x > 40)将返回相同的形状的一个布尔阵列xTrue ,其中的元素x是大于40。

Note the use of | 注意使用| for bitwise-or and ~ for not (boolean negation). for bitwise-和~ for not (boolean negation)。


Alternatively, by De Morgan's law , you could use 或者,根据德摩根定律 ,您可以使用

mask = ((x <= 40) & (x >= -40) & (y <= 20) & (y >= -20))

NumPy operations are performed element-wise . NumPy操作是按元素执行 So mask is True whereever an element of x is between -40 and 40 and the corresponding element of y is between -20 and 20. 因此,如果x的元素在-40和40之间, 并且 y 的相应元素在-20和20之间,则mask为True。


For example, 例如,

import numpy as np
x = [-50, -50, 30, 0, 50]
y = [-30, 0, 10, 30, 40]

# change the lists to NumPy arrays
x, y = np.asarray(x), np.asarray(y)
# mask = ~((x > 40) | (x < -40) | (y > 20) | (y < -20))
mask = ((x <= 40) & (x >= -40) & (y <= 20) & (y >= -20))
x, y = x[mask], y[mask]

yields 产量

In [35]: x
Out[35]: array([30])

In [36]: y
Out[36]: array([10])

with

In [37]: mask
Out[37]: array([False, False,  True, False, False], dtype=bool)

Another option is to work with list-comprehension : 另一种选择是使用list-comprehension

Input : 输入

x = [50, 10, -50, 30, 5, 6]
y = [2, 40, 10, 5, 3, 5]

Code : 代码

x, y = list(zip(*[(x, y) for x, y in zip(x, y) if x <= 40 and x > -40 and y <= 20 and y > -20]))

Output : 输出

x
# (30, 5, 6)

y
# (5, 3, 5)

Try this: 尝试这个:

mask = ((x <= 40) & (x >= -40) & (y <= 20) & (y >= -20))
x, y = x[mask], y[mask]

NumPy will vectorize these operations, so it should be very efficient. NumPy将对这些操作进行矢量化,因此它应该非常高效。

This blog post might be helpful, and here's the manual for np.where() which shows some similar examples. 这篇博文可能会有所帮助,这里是np.where()的手册,其中显示了一些类似的例子。

This should do it. 这应该做到这一点。

for i in x1:
    if i <= 40 and i >= -40:
        print "True"
        for i in y1:
            if i <=20 and i >=-20:
                print "True"
            else:
                x1.remove(i)
                y1.remove(i)
    else:
        x1.remove(i)
        y1.remove(i)

Hope this helped! 希望这有帮助!

Thanks! 谢谢!

For completeness, here is a solution based on itertools . 为了完整起见 ,这是一个基于itertools的解决方案。

Consider the following lists of coordinates: 请考虑以下坐标列表:

x = [-50, -50, -10,   0,  10, 50, 50, -10, -10, 0, 10, 40]
y = [-50, -20, -50,  50, -50, 20, 50, -20, -10, 0, 20, 10]

Our goal is to set-up a boolean mask with the value True in those indices n for which x[n] and y[n] lie in certain intervals, and False elsewhere. 我们的目标是建立一个布尔掩码与所述值True在这些索引n为其x[n]y[n]在于一定的间隔,和False别处。 The intervals' boundaries are: 区间的边界是:

xmin, xmax = -40, 40
ymin, ymax = -20, 20

We can create such a mask through a list comprehension: 我们可以通过列表理解创建这样的掩码:

mask = [xmin <= i <= xmax and ymin <= j <= ymax for (i, j) in zip(x, y)]

The boolean expression xmin <= i <= xmax and ymin <= j <= ymax is evaluated for each pair of corresponding coordinates. 针对每对相应坐标评估布尔表达式xmin <= i <= xmax and ymin <= j <= ymax If both i and j belong to the specified intervals the expression is evaluated as True , and False otherwise. 如果ij属于指定的时间间隔,则表达式将被计算为True ,否则为False The fact that comparisons can be chained in Python makes this boolean expression pretty compact and readable. 比较可以在Python中链接的事实使得这个布尔表达式非常紧凑和可读。

Finally, we can get rid of those coordinate pairs that fall outside limits using the function itertools.compress() : 最后,我们可以使用函数itertools.compress()去掉那些超出限制的坐标对:

from itertools import compress
x_clipped = list(compress(x, mask))
y_clipped = list(compress(y, mask))

Demo: 演示:

In [117]: mask
Out[117]: [False, False, False, False, False, False, False, True, True, True, True, True]

In [118]: x_clipped
Out[118]: [-10, -10, 0, 10, 40]

In [119]: y_clipped
Out[119]: [-20, -10, 0, 20, 10]

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

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