Easy way to test if each element in an numpy array lies between two values?

前端 未结 3 770
名媛妹妹
名媛妹妹 2020-12-02 13:02

I was wondering if there was a syntactically simple way of checking if each element in a numpy array lies between two numbers.

In other words, just as numpy.ar

3条回答
  •  没有蜡笔的小新
    2020-12-02 13:28

    one solution would be:

    a = numpy.array([1,2,3,4,5])
    (a > 1).all() and (a < 5).all()
    

    if you want the acutal array of truth vaues, just use:

    (a > 1) & (a < 5)
    

提交回复
热议问题