python编程统计两个数组连续3次及以上大于阈值的位置,若两个数组连续大于阈值的位置重合3次及以上,记为n,否则为k
时间: 2024-03-10 16:50:59 浏览: 11
可以通过以下代码实现:
```python
def count_position(arr1, arr2, threshold):
n = 0 # 两个数组连续大于阈值的位置重合3次及以上
k = 0 # 两个数组连续大于阈值的位置未重合3次及以上
length = min(len(arr1), len(arr2))
for i in range(length - 2):
if arr1[i] > threshold and arr1[i+1] > threshold and arr1[i+2] > threshold \
and arr2[i] > threshold and arr2[i+1] > threshold and arr2[i+2] > threshold:
if i <= length - 5 and arr1[i+3] > threshold and arr1[i+4] > threshold and arr1[i+5] > threshold \
and arr2[i+3] > threshold and arr2[i+4] > threshold and arr2[i+5] > threshold:
n += 1
i += 5
else:
k += 1
return n if n >= 3 else k
```
其中,arr1和arr2分别为两个数组,threshold为阈值。函数中的思路是遍历两个数组,判断当前位置及其后续两个位置是否都大于阈值,如果是,则判断后续三个位置是否也都大于阈值,如果是,则认为这两个数组在该位置及其后续5个位置连续大于阈值,计数器n加1,同时将i加5,跳过这6个位置的判断;如果后续三个位置不都大于阈值,则认为这两个数组在该位置及其后续2个位置连续大于阈值,计数器k加1。最后,如果n大于等于3,则返回n,否则返回k。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)