gpt4 book ai didi

python - 从 numpy 数组中删除一些数组元素

转载 作者:行者123 更新时间:2023-12-02 02:25:18 25 4
gpt4 key购买 nike

我有 numpy 数组:

a = np.array([[ 255,255,255],
[ 255,2,255],
[ 3,123,23],
[ 255,255,255],
[ 0, 255, 3]])

我想删除所有带有[ 255,255,255]的元素,结果应该是:

[[ 255,2,255],
[ 3,123,23],
[ 0, 255, 3]])

我尝试过:

import numpy as np
a = np.array([[ 255,255,255],
[ 255,2,255],
[ 3,123,23],
[ 255,255,255],
[ 0, 255, 3]])

np.delete(a, [255,255,255])

但什么也没发生。

最佳答案

你可以这样做:

np.array([x for x in a if np.any(x != 255)])

给出:

array([[255,   2, 255],
[ 3, 123, 23],
[ 0, 255, 3]])

编辑:避免列表理解 -

np.delete(a, np.where((a == 255).all(axis=1)), axis=0)

关于python - 从 numpy 数组中删除一些数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58857363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com