列出数字之间的差异

时间:2012-07-16 22:51:22

标签: python numpy python-2.7

我有两次输入从最小到最大的有序数据。

 start_time[s] = [10, 20, 30, 40, 50, 61, 79, 80]

 end_time[s] = [8, 9, 15, 31, 41, 60]

列表与生成日志文件时间戳数据的大小不同

我想得到end_time和start_time

之间正差异的输出

我的代码如下:

  for item1 in end_time:   
    for item2 in start_time:  
      if (item1 > item2):
         new_item = item1 - item2
         new_list.append(new_item)
  
    
      

[5,21,11,1,31,21,11,1,50,50,30,20,10]

    
  

理想输出将按如下方式生成:

  
    
      

[5,11,11,20]

    
  

5 ...这是通过15的end_time - 10的start_time,为什么?它的第一个end_time> start_time(8,9也是end_times小于10)

11 ...这是通过采取31的下一个end_time(我不想使用15,因为我将重复计算)然后减去20的下一个start_time给11。

11 ...这是通过将以下end_time取为41并将start_time减去30来得到11。

20 ......这将是最后一个条目,它从end_time开始需要60,并且从start_time开始使用40来产生20的差异。

2 个答案:

答案 0 :(得分:0)

我假设两个列表都是有序的,这就是您的示例数据显示的内容。

timeDeltas = []
idxStart = 0
idxEnd =  len(end_time)
for start in start_time:
    for i in range(idxStart, idxEnd)
        delta = 0
        end = end_time[i]
        if end > start:
            delta = end - start
            idxStart = i
        else:
            timeDeltas.append(delta)
            break

请注意,我没有进行健全性检查(例如,可以反复使用相同的结束时间)。

答案 1 :(得分:0)

我就这样做了

start_time = [10, 20, 30, 40, 50, 61, 79, 80]
end_time = [8, 9, 15, 31, 41, 60]
time_difference = [(min(start_time) - et) for et in end_time if min(start_time) > et]
[2,1]

您的问题可能有点含糊不清。我假设你严格要求积极的时差。

您想要的输出是什么?