按时间间隔排序列表

时间:2019-06-13 16:12:08

标签: python list

我有两个列表:

time_sentence = 
[[11000, 'sen1'], [18000, 'sen2'], [24000, 'sen3'], [40000, 'sen4'], [54000, "sen5"], [61000, "sen6"]]


time_intervalls = [11000, 25000,50000,70000]

第一个列表是(时间,字符串)的2D数组,第二个列表是特定时间戳的列表。现在,我想知道如何根据两个时间戳之间的时间提取所有属于它们的句子?

预期结果:

sentences= [['sen1','sen2','sen3'],['sen4'],['sen5','sen6']]

1 个答案:

答案 0 :(得分:1)

您可以尝试一下,

time_sentence = [[11000, 'sen1'], [18000, 'sen2'], [24000, 'sen3'], [40000, 'sen4'], [54000, "sen5"], [61000, "sen6"]]
time_intervals = [11000, 25000, 50000, 70000]

l = len(time_intervals)
sentences = []

for index,obj in enumerate(time_intervals):

    current = obj
    sentence = []

    if index < (l - 1):
        next_ = time_intervals[index + 1]

        for row in range(len(time_sentence)):
            if current <= time_sentence[row][0] <= next_:
                sentence.append(time_sentence[row][1])

        if sentence != []:
            sentences.append(sentence)

print(sentences)

参考:https://stackblitz.com/edit/angular-format-today-date?file=src/app/app.component.html