简体   繁体   English

在Python 2.7中循环数据

[英]Cycling data in Python 2.7

I am trying to detect phase times for an oscillating object, but I can't seem to figure out how to keep my previous three data points stored at all times. 我正在尝试检测振荡对象的相位时间,但似乎无法弄清楚如何始终存储我之前的三个数据点。

prev2 == prev
prev == current
current == int(data)

Every time I go through a data collection loop, I get some value for data, and I expect if I go through this loop 3 times, I should have my previous three values for data stored as my variables, but for some reason current, prev, and prev2 all stay at 0. What makes even less sense to me, is that if I put: print(int(data)) directly below all of this, it will return the number I want to go into current. 每次经过数据收集循环时,我都会得到一些数据值,并且我希望如果经过3次循环,我应该将之前的三个数据值存储为变量,但是由于某种原因,当前为prev和prev2都保持为0。对我而言,更没有意义的是,如果我在所有这些代码的正下方放置: print(int(data)) ,它将返回我要输入的当前数字。 If anyone would know how I could fix this, I would greatly appreciate it. 如果有人知道我该如何解决,我将不胜感激。

You want to use = instead of == . 您要使用=而不是== == checks for equality while = is an assignment statement. ==检查是否相等,而=是赋值语句。 Your code should be: 您的代码应为:

prev2 = prev
prev = current
current = int(data)

This is an excellent opportunity to introduce you to the deque . 这是向您介绍deque的绝好机会。

>>> from collections import deque
>>>
>>> rolling = deque([5,6,7], maxlen=3)
>>> rolling
deque([5, 6, 7], maxlen=3)
>>> rolling.append(8)
>>> rolling
deque([6, 7, 8], maxlen=3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM