我有 5000 多行的数据集,根据任何给定数据行中存在的元素,长度各不相同。我已将每一行数据存储为 list,其中每个元素都存储为 index(即某些行可能有 5 个索引,而其他行可能有 20 个)。索引 2 始终是该行数据耗时。
我的脚本目前迭代数据集,使用for循环作为构建数据的方法。见下文:
f = open(fn)
for line in iter(f):
remove_blanks = ['']
entries = line.split()
''.join([i for i in entries if i not in remove_blanks])
trash = (entries[0], entries[1])
time = int(entries[2]) # Time expressed as elapsed time in milliseconds
column = [int(v) for v in entries[3:]]
脚本迭代的行(来自数据集)看起来像
x y #time a b
d e #time f g h
然后将其放入列表中
['x', 'y', '#time', 'a', 'b']
['d', 'e', '#time', 'f', 'g', 'h']
我想从任何给定行中出现的时间迭代地减去初始时间(非零)。例如,我想做 (#time on line x) - (#time on line 0) 并打印每一行的运行时间。也许这可以在创建数据结构之前完成,我不确定。
请帮忙!
谢谢
请您参考如下方法:
你可以这样做
f = open(fn)
t0 = None
for line in iter(f):
remove_blanks = ['']
entries = line.split()
''.join([i for i in entries if i not in remove_blanks])
trash = (entries[0], entries[1])
time = int(entries[2]) # Time expressed as elapsed time in milliseconds
if t0 is None: # first iteration, store time at t0
t0 = time
else:
print("Elapsed time: {}ms".format(time - t0)) # every iteration except the first, print elapsed time
column = [int(v) for v in entries[3:]]
它会在每次迭代时打印耗时。我认为这就是您寻求帮助的原因
