Python 在重叠的数组中组合数字持续时间项

Python 在重叠的数组中组合数字持续时间项,python,python-3.x,algorithm,Python,Python 3.x,Algorithm,我有一个[starttime in seconds,duration in seconds]数组,如下所示 [0.2, 0.4] [0.3, 0.5] [0.6, 0.5] [10.6, 0.5] 我想合并重叠时间,即前面提到的数组应转换为 [0.2, 0.9] [10.6, 0.5] 什么是优雅的解决方案?假设您有包含所有输入数据的列表,并且它们按开始时间排序 a=[[0.2,0.4],[0.3,0.5],[0.6,0.5],[10.6,0.5]] # each element is

我有一个
[starttime in seconds,duration in seconds]
数组,如下所示

[0.2, 0.4]
[0.3, 0.5]
[0.6, 0.5]
[10.6, 0.5]
我想合并重叠时间,即前面提到的数组应转换为

[0.2, 0.9]
[10.6, 0.5]

什么是优雅的解决方案?

假设您有包含所有输入数据的列表,并且它们按开始时间排序

   a=[[0.2,0.4],[0.3,0.5],[0.6,0.5],[10.6,0.5]] # each element is list of form [start,duration]
   output =[] # output would be stored here
   for x in a: # iterate through input
    if output: # if we have previous output to compare
        st = x[0] # current start time
        dur = x[1] # current durtion time
        prev_st = output[-1][0] # previous computed output start time
        prev_dur = output[-1][1] # previous computed output duration
        if prev_st<=st<= (prev_st+prev_dur): # check if current info can be part of previous output
            updated_list=[] Update the latest output with new info
            updated_list.append(prev_st) #start would remain the same as it is sorted
            updated_list.append(max(prev_dur,st+dur-prev_st)) # duration can be different as per current info
            output[-1] = updated_list
        else:
            output.append(x)
    else:
        output.append(x) # append first list as it is
a=[[0.2,0.4]、[0.3,0.5]、[0.6,0.5]、[10.6,0.5]#每个元素都是表单列表[开始,持续时间]
输出=[]#输出将存储在此处
对于a中的x:#迭代输入
如果输出:#如果我们有以前的输出进行比较
st=x[0]#当前开始时间
dur=x[1]#当前持续时间
上一次=输出[-1][0]#上一次计算的输出开始时间
prev_dur=输出[-1][1]#上一次计算的输出持续时间

如果上一次仍然建议先0.6+0.5=1.1-->1.1-0.2=0。9@cryptonome没有代码来重现问题Fabien,是你请求帮助的。想想看&如果角色颠倒,请求是不合理的。@cryptonome你和我一样知道,人们写答案的动机是双重的。首先,他们喜欢挑战,解决问题后感觉良好;其次,他们希望许多人看到他们的回答,从而获得好的工作机会。这两个人都没有受到我所做的事情的影响。最后,大多数情况下,正是那些需要示例的人,一旦给出或没有给出解决方案,就不会编写。就像这里发生的那样,你的解决方案cryptonome和jpp在哪里?