gpt4 book ai didi

python - 如何使用另一个数组屏蔽一个数组

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:18 25 4
gpt4 key购买 nike

我有一个 items 数组,其形状为 (n, 3),还有一个 counts 数组,其形状相同 (n, 3)。如何在不使用循环的情况下使 items 中的每个 C 都具有 count = 0

items = np.array([['B', 'A', 'C'],
['B', 'B', 'C'],
['A', 'B', 'A'],
['C', 'C', 'C'],
['B', 'B', 'B']])

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

预期输出:

>>> counts
np.array([[1, 3, 0],
[4, 2, 0],
[2, 2, 1],
[0, 0, 0],
[1, 2, 1]])

最佳答案

你要找的是:

counts[items == 'C'] = 0

更明确的表达方式是:

c_indices = np.where(items == 'C')
counts[c_indices] = 0

关于python - 如何使用另一个数组屏蔽一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57155499/

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