如何创建一个比较多行并打印时间戳的for / if循环

时间:2018-01-21 12:30:08

标签: python dataframe timestamp data-science

我目前正在尝试开发一个比较行内数据的代码。该程序的目的是比较速度的差异,如果速度有很大差异,我希望它打印时间戳。这是我到目前为止写的:

for row in cj1:
speed = row[1].speed
timestamp = row[0].timestamp
for i in range(1, row.length):
if speed is different =!:
      Calculate acceleration
if acceleration > #certain amount:
   print(timestamp)
Else:
   timestamp = rows[i].timestamp
   speed = rows[i].speed

这是一个非常粗略的代码,我不太清楚如何表达它。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

如果您只想计算代码需要多长时间。您可以使用此上下文管理器示例。

from time import time, sleep

class Time(object):
    def __enter__(self):
        self.start = time()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.end = time()
        self.interval = self.end - self.start


with Time() as t:
    sleep(5)  # <- your code here

print(t.interval)

<强> 5.001040935516357

<强>更新

所以下次你可以问一个更好的问题。以下是一些反馈:

for row in cj1:
    speed = row[1].speed                # <- There was no indent here.
    timestamp = row[0].timestamp
    for i in range(1, row.length):
        if speed is different =!:       # <- There was no indent here.
                                        # `=!` with out another value is a syntax error.
              Calculate acceleration    #  <- `Calculate` is not defined.
                                        #  Also no brackets are used,
                                        #  maybe `Calculate(acceleration)`?
        if acceleration > #certain amount:

                                        # `acceleration` is not defined.
                                        # You can not compare greater than with a comment.
           print(timestamp)
        Else:                           # < - `Else` cannot be capitalised. you mean. `else`
           timestamp = rows[i].timestamp
           speed = rows[i].speed

答案 1 :(得分:0)

也许您应该展示一段数据,它看起来如何? 值的格式是什么?
您显示的代码有错误的缩进等。我认为数据不能像行[1] .speed一样读取,或者这确实有用吗?

编辑: 那是计算某种伪代码吗?请尝试首先在Python shell中制定单行,看它们是python代码......