[英]Python: count how many times an element of a vector is in between of elements of two other vectors?
Say I have 3 vectors: 假设我有3个向量:
v1 = 1,6,7
v2 = 2,5,6
v3 = 3,4,2
I want to count how many times that v1[i] <= v2[i] <= v3[i]
(in a Pythonic way of course). 我想计算
v1[i] <= v2[i] <= v3[i]
(以Pythonic的方式)的次数。 For the above example, the answer should be 1
. 对于以上示例,答案应为
1
。
尝试这个:
sum(v1[x] <= v2[x] <= v3[x] for x in range(3))
如果v1
, v2
等是numpy.arrays
,则可以执行
np.sum(np.logical_and(v1<v2, v2<v3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.