【发布时间】:2018-10-04 06:07:38
【问题描述】:
我有一个 numpy 数组和一个列表。我想删除列表中包含的行。
a = np.zeros((3, 2))
a[0, :] = [1, 2]
l = [(1, 2), (3, 4)]
目前我尝试通过创建一组a 的行来做到这一点,然后排除从l 创建的set,类似于:
sa = set(map(tuple, a))
sl = set(l)
np.array(list(sa - sl))
或者更简单
sl = set(l)
np.array([row for row in list(map(tuple, a)) if row not in sl]
当每一行都很短时,这些效果很好。
有没有更快的方法?我需要优化速度。
【问题讨论】:
-
看看
np.lib.arraysetops函数 -
@hpaulj 我明白了,
np.lib.arraysetops是通过排序模拟的set。但是与直接在python中创建集合相比如何?我想对于长数组和列表,使用set更快?
标签: python arrays performance numpy set