在列表中填写缺失值

时间:2018-05-03 18:02:52

标签: python

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

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

我想在剩下的时间里填写缺失的时间戳,并且有一个' 0'假设时间与数字sin list1和list2匹配,则任何时候都没有时间戳的核心编号。所以我们得到:

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 个答案:

答案 0 :(得分: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))

答案 1 :(得分: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)

答案 2 :(得分:0)

您可以将list1中的时间戳映射到list2中的现有值。然后,在输入中找到最大时间戳,并使用结果创建全部新时间戳。通过使用原始映射,您可以遍历范围,在迭代时为当前值创建时间戳,并尝试访问字典中的相应值。通过使用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']

答案 3 :(得分:-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]

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