3

假设我有 2 个与时间戳和数字相关的数据列表,因此:

list1 = ['00:00:02', '00:00:05', '00:00:06']
list2 = [2,3,4]

假设时间与数字 sin list1 和 list2 匹配,我想基本上将缺失的时间戳填充到一天的其余时间,并在时间戳没有对应数字的任何时间使用“0”。所以我们得到:

list1 = ['00:00:02', '00:00:05', '00:00:06']
list2 = [2,3,4]

list3 = ['00:00:00', '00:00:01', '00:00:02', '00:00:03', '00:00:04', '00:00:05', '00:00:06']
list4 = [0,0,2,0,0,3,4]

一直试图概念化如何做到这一点,任何人都想不出任何合乎逻辑的事情。

4

4 回答 4

2

您可以使用生成器来生成每一对,而不是硬编码列表:

from functools import reduce

def base60(ts):
    """Parses the timestamps HH:MM:SS into monotonic integer numbers"""
    return reduce(lambda acc, v: acc * 60 + int(v), ts.split(":"), 0) 

def to_ts(v):
    """Convert a single integer representing a "base 60" HH:MM:SS timestamp into a timestamp sting"""
    parts = []
    while v:
        parts.insert(0, "{:02d}".format(v % 60))
        v //= 60
    return ":".join((["00"] * (3 - len(parts))) + parts)

def timeseries(timestamps, values):
    counter = 0
    for timestamp, value in zip(timestamps, values):
        target = base60(timestamp)
        while counter < target:
            # While internal counter is less than the next
            # timestamp in the given series, 
            # yield the counter and a value of zero. 
            yield to_ts(counter), 0
            counter += 1
        yield timestamp, value
        counter += 1

如果您确实需要将结果作为两个单独的静态序列(尽管 zip 产生元组而不是列表):

list3, list4 = zip(*timeseries(list1, list2))
于 2018-05-03T18:28:14.117 回答
1

通过将您的数据放入dict. 感谢 Aran-Fey 提高了效率。

import time

list1 = ['00:00:02', '00:00:05', '00:00:06']
list2 = [2,3,6]

def get_sec(time_str):
    h, m, s = time_str.split(':')
    return int(h) * 3600 + int(m) * 60 + int(s)

timestamps = dict(zip(list1, list2))
list3 = []
list4 = []

for t in range(get_sec(list1[-1])+1):
    time_str = time.strftime('%H:%M:%S', time.gmtime(t))
    num = timestamps.get(time_str, 0)

    list3.append(time_str)
    list4.append(num)

print(list3, list4)
于 2018-05-03T18:18:39.913 回答
0

您可以将时间戳映射list1list2. 然后,找到输入中的最大时间戳,并使用结果创建完整范围的新时间戳。通过使用原始映射,您可以迭代范围,在迭代时为当前值创建时间戳,并尝试访问字典中的相应值。通过使用dict.get,如果原始输入中不存在生成的时间戳,则可以应用填充值:

import re
list1 = ['00:00:02', '00:00:05', '00:00:06']
list2 = [2,3,4]
def create_stamp(d):
  s = '0'*(6-len(d))+d
  return ':'.join(s[i:i+2] for i in range(0, len(s), 2))

def fill_stamps(a, b, fill_val = 0):
  original = dict(zip(a, b))
  max_v = int(''.join(re.findall('\d+', max(a, key=lambda x:int(''.join(re.findall('\d+', x)))))))
  return [original.get(create_stamp(str(i)), fill_val) for i in range(max_v+1)], [create_stamp(str(i)) for i in range(max_v+1)]

list4, list3 = fill_stamps(list1, list2)

输出:

[0, 0, 2, 0, 0, 3, 4]
['00:00:00', '00:00:01', '00:00:02', '00:00:03', '00:00:04', '00:00:05', '00:00:06']
于 2018-05-03T19:22:13.677 回答
-1

编辑:根据评论和帖子编辑进行修复(使用相当松散的固定术语......)

我不会撒谎说这是最好的做事方式,但也许为了帮助你思考问题,你可以试试这个:

# Starting lists
list1 = ['00:00:02', '00:00:05', '00:00:06']
list2 = [2,3,4]
# create a list that looks like [2,5,6] from list1[]
list1_linker = []
for x in list1:
  list1_linker.append(int(x.split(":")[2]))
# Output lists
new_list1 = []
new_list2 = []
# Temp lists
temp_list1 = []
temp_list2 = []
# Build temps
for i in range(0,max(list1_linker)+1):
  temp_time_stamp = '00:00:'
  if i < 10:
    temp_time_stamp += '0'
    temp_time_stamp += str(i)
    temp_list1.append(temp_time_stamp)
  else:
    temp_time_stamp += str(i)
    temp_list1.append(temp_time_stamp)
  temp_list2.append(i)
# Build output 1
new_list1 = temp_list1
# Build output 2
for x in range(0,len(temp_list2)):
  if x in list1_linker:
    new_list2.append(list2.pop(0))
  else:
    new_list2.append(0)
# Print our lists    
print(new_list1)
print(new_list2)

输出:

['00:00:00', '00:00:01', '00:00:02', '00:00:03', '00:00:04', '00:00:05', '00:00:06']
[0, 0, 2, 0, 0, 3, 4]

现在再一次,有更好的方法来实现这一点,但根据问题中的信息,这至少应该可以帮助你思考问题

于 2018-05-03T18:21:28.437 回答