计算一个系列中值变化的次数 [英] Count the number of times values change in a series

查看:52
本文介绍了计算一个系列中值变化的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑系列:

s = [1, -1, 1, 1, 1, -1]

计算此类系列中值更改次数的最省时方法是什么?在这个例子中,答案是 3(从 1 到 -1,回到 1,再到 -1)

What is the most time-efficient way to count the number of times values in such series change? In this example, the answer is 3 (from 1 to -1, back to 1, and again to -1)

推荐答案

我将使用 numpy

(np.diff(s)!=0).sum()
Out[497]: 3

这篇关于计算一个系列中值变化的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆