美文网首页
python技巧|np.where配合区间使用

python技巧|np.where配合区间使用

作者: reallocing | 来源:发表于2019-01-23 11:34 被阅读0次
    Here are two ways:
    
    In [1]: my_array = arange(10)
    
    In [2]: np.where((my_array > 3) & (my_array < 7))
    Out[2]: (array([4, 5, 6]),)
    
    In [3]: np.where(logical_and(my_array > 3, my_array < 7))
    Out[3]: (array([4, 5, 6]),)
    For the first (replacing and with &), be careful to add parentheses appropriately:  & has higher precedence than the comparison operators. You can also use *, but I wouldn't recommend it: it's hacky and doesn't make for readable code.
    
    In [4]: np.where((my_array > 3) * (my_array < 7))
    Out[4]: (array([4, 5, 6]),)
    

    相关文章

      网友评论

          本文标题:python技巧|np.where配合区间使用

          本文链接:https://www.haomeiwen.com/subject/tbdjjqtx.html