【发布时间】:2017-08-01 16:23:32
【问题描述】:
我是一名 Python(和整个编程)新手。我已经看到了数百个关于此的问题,但都使用pandas 包进行数据分析。我已经有数百行代码没有使用pandas,所以我想尽可能避免使用pandas,或者如果我可以使用pandas 重构我的数据,我愿意接受建议。
我有几千行原始数据被列成entries 的列表。我使用column 从entries 创建另一个列表,以便于执行我的函数。
我想从当前行的同一索引中减去前一行的索引 (entries[2])。我看到了两种完成方式:
当前行entries[2] - 上一行entries[2]
或
当前行Elapsed Time - 上一行Elapsed Time
样本原始数据:
1 c 4977321 200 200 007 003 033 001 002 003 092 001
2 d 4977789 010 120 100 100
3 e 4977816 175 194 000
4 f 4977868 225 220 100 300 001
这里,索引 2 是时间(即 4977321)
我的代码如下所示:
f = input('Type in File Name: ') # What Raw Data log do we want to analyze?
def function():
print_string = ''
# do something with the data
print_string += 'Time Delta: ' + str(delta_time)
if 'Time Delta' in print_string:
print(print_string)
initial_time = None # Establishes 0 start point for time (not all captures start at 0ms)
with open(f, "r") as f:
for line in f:
entries = line.split() # Organizes data line as a list, entries as indices
column = [int(v) for v in entries[3:]]
delta_time = int(entries[2]) - time
time = int(entries[2]) # Time expressed in milliseconds
if initial_time is None:
initial_time = time
delta_time = 0
elap_time = time - initial_time # Expressed as elapsed time from start of capture in milliseconds
function()
f.close()
我省略了大部分代码主体(整个函数),因为我没有看到它是必要的信息。
我希望代码确定delta_time 的值,以便在执行函数后打印。我希望输出看起来像:
Time Delta: # (nothing here, no previous line)
Time Delta: # 4977789 - 4977321
Time Delta: # 4977816 - 4977789
Time Delta: # 4977868 - 4977816
仅供参考,我在打印时也在我的函数中使用了elap_time,只是没有在此处包含它。
【问题讨论】:
-
为什么要不用pandas来折磨自己?
-
@coldspeed 很长一段时间我都在安装软件包时遇到问题。直到最近我才终于解决了这个问题并安装了 pandas。在我看来,现在回去为时已晚。虽然熊猫会为我节省数小时的编码/学习时间,哈哈。
标签: python list data-structures time