gpt4 book ai didi

python - 使用 2 个条件在 Python 中过滤数组

转载 作者:行者123 更新时间:2023-11-28 19:56:23 26 4
gpt4 key购买 nike

如何根据两个条件过滤数组A?

A = array([1, 2.3, 4.3, 10, 23, 42, 23, 12, 1, 1])
B = array([1, 7, 21, 5, 9, 12, 14, 22, 12, 0])
print A[(B < 13)] # here we get all items A[i] with i such that B[i] < 13
print A[(B > 5) and (B < 13)] # here it doesn't work
# how to get all items A[i] such that B[i] < 13
# AND B[i] > 5 ?

错误是:

ValueError: The truth value of an array with more than one element is ambiguous.

最佳答案

您应该使用运算符 and按位(感谢@askewchan)版本,即 &

 print A[(B > 5) & (B < 13)] 

关于python - 使用 2 个条件在 Python 中过滤数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20072030/

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