Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中从列表列表中提取插槽_Python_List_Sorting_Iteration - Fatal编程技术网

在python中从列表列表中提取插槽

在python中从列表列表中提取插槽,python,list,sorting,iteration,Python,List,Sorting,Iteration,我有以下格式的列表,需要传递给api [ [0, 4, 0, 4, 59], [0, 5, 0, 5, 59], [0, 6, 0, 6, 59], [0, 13, 0, 13, 59], [0, 14, 0, 14, 59], [0, 21, 0, 21, 59], [0, 22, 0, 22, 59], [1, 5, 0, 5, 59], [1, 6, 0, 6, 59], [1, 13, 0, 13, 59], [1, 14, 0, 14, 59], [1

我有以下格式的列表,需要传递给api

[
    [0, 4, 0, 4, 59], [0, 5, 0, 5, 59], [0, 6, 0, 6, 59], [0, 13, 0, 13, 59],
    [0, 14, 0, 14, 59], [0, 21, 0, 21, 59], [0, 22, 0, 22, 59], 
    [1, 5, 0, 5, 59], [1, 6, 0, 6, 59], [1, 13, 0, 13, 59], [1, 14, 0, 14, 59],
    [1, 21, 0, 21, 59], [1, 22, 0, 22, 59], [2, 5, 0, 5, 59], [2, 6, 0, 6, 59], 
    [2, 13, 0, 13, 59], [2, 14, 0, 14, 59], [2, 21, 0, 21, 59], 
    [2, 22, 0, 22, 59], [3, 5, 0, 5, 59], [3, 6, 0, 6, 59], [3, 13, 0, 13, 59],
    [3, 14, 0, 14, 59], [3, 21, 0, 21, 59], [3, 22, 0, 22, 59], 
    [4, 5, 0, 5, 59], [4, 6, 0, 6, 59], [4, 13, 0, 13, 59], [4, 14, 0, 14, 59],
    [4, 21, 0, 21, 59], [4, 22, 0, 22, 59], [5, 5, 0, 5, 59], [5, 6, 0, 6, 59],
    [5, 13, 0, 13, 59], [5, 14, 0, 14, 59], [5, 21, 0, 21, 59], 
    [5, 22, 0, 22, 59], [6, 5, 0, 5, 59], [6, 6, 0, 6, 59], [6, 13, 0, 13, 59],
    [6, 14, 0, 14, 59], [6, 21, 0, 21, 59], [6, 22, 0, 22, 59] 
]
在每个列表中,第一个元素表示一天,接下来的元素表示小时和分钟到小时和分钟。从上面的示例中,对于第0天,slot1是04:00到6:59,slot2是13:00到14:59,slot3是21:00到22:59

我试图将列表简化为以下内容

[0, 04:00, 6:59, 13:00, 14:59, 21:00, 22:59]....
从本质上讲,提取每天的小时时段并将其组合到单个列表中,因此最终输出从第0天到第6天只有7个列表

另外请注意,上述格式可能会更改,对于任何给定的一天,可能只有一个插槽,也可能没有插槽,因此每天的插槽可能在0-3之间变化

到目前为止,我设法加入了以下小时和分钟 `


您可以这样做:

input = [[0, 4, 0, 5, 59],[0, 5, 0, 5, 59], [0, 6, 0, 6, 59], [0, 13, 0, 13, 59], [0, 14, 0, 14, 59], [0, 21, 0, 21, 59], [0, 22, 0, 22, 59], [1, 5, 0, 5, 59], [1, 6, 0, 6, 59], [1, 13, 0, 13, 59], [1, 14, 0, 14, 59], [1, 21, 0, 21, 59], [1, 22, 0, 22, 59], [2, 5, 0, 5, 59], [2, 6, 0, 6, 59], [2, 13, 0, 13, 59], [2, 14, 0, 14, 59], [2, 21, 0, 21, 59], [2, 22, 0, 22, 59], [3, 5, 0, 5, 59], [3, 6, 0, 6, 59], [3, 13, 0, 13, 59], [3, 14, 0, 14, 59], [3, 21, 0, 21, 59], [3, 22, 0, 22, 59], [4, 5, 0, 5, 59], [4, 6, 0, 6, 59], [4, 13, 0, 13, 59], [4, 14, 0, 14, 59], [4, 21, 0, 21, 59], [4, 22, 0, 22, 59], [5, 5, 0, 5, 59], [5, 6, 0, 6, 59], [5, 13, 0, 13, 59], [5, 14, 0, 14, 59], [5, 21, 0, 21, 59], [5, 22, 0, 22, 59], [6, 5, 0, 5, 59], [6, 6, 0, 6, 59], [6, 13, 0, 13, 59], [6, 14, 0, 14, 59], [6, 21, 0, 21, 59], [6, 22, 0, 22, 59]]

output = {}
for row in input:
    key = row[0]
    output.setdefault(key, [str(key)])
    output[key].append('%d:%02d' % (row[1], row[2]))
    output[key].append('%d:%02d' % (row[3], row[4]))

result = output.values()

我还假设您希望: (a) 合并连续约会(即前一次约会结束后立即开始) (b) 按照上面所示的方式格式化这些文件 (c) 在平展列表中按天对这些进行分组

如果是,可以使用以下方法解决此问题:

(a) 制作一个助手函数,将日、小时和分钟变量转换为分钟:

def get_minute_val(day_val,hour_val,min_val):
    return (24*60*day_val)+(60*hour_val)+min_val
(b) 创建一个函数,该函数接受两个约会,如果它们是连续的,则将它们合并为一个约会;如果它们不是连续的,则返回未组合的约会

def combine_if_consec(first,second):
    #Check whether appointments are consecutive
    if( get_minute_val(first[0],first[3],first[4]) + 1 == 
        get_minute_val(second[0],second[1],second[2])):
        #If so, return list containing combined appointment
        return [[first[0],first[1],first[2],second[3],second[4]]]
    else:
        #Else return uncombined appointments
        return [first,second]
(c) 在列表中的每个约会上迭代调用此选项,并与最近添加的约会进行比较。我有一个处理第一次约会的方法

def combine_all_appointments(app_list):
    #Add first appointment to app list
    output_list = [test[0]]

    #Loop through remaining appointments
    for next_app in app_list[1:]:
        #Remove most recent appointment to output list
        prev_app = output_list.pop()

        #Add either 2 combined appointments, or one single appointment to outputlist
        output_list += combine_if_overlap(prev_app,next_app)

    return output_list
(d) 创建一个函数,该函数执行所需的格式设置

def format_appointments(app_list):
    return [[x[0],'%d:%02d' % (x[1],x[2]),'%d:%02d' %(x[3],x[4])] for x in app_list]
(e) 另外一个是按天分组预约,按天平铺

def group_by_day(app_list):
    output = {}
    #Loop through appointments
    for app in app_list:
        #Create new entry if day not yet in output dict
        if app[0] not in output:
            output[app[0]] = app[1:]
        #Add appointment values to relevant day
        else:
            output[app[0]] += app[1:]
    #Flatten dictionary
    return [[k, *output[k]] for k in output]
在您的输入上测试:

test = [[0, 4, 0, 4, 59],[0, 5, 0, 5, 59], [0, 6, 0, 6, 59], [0, 13, 0, 13, 59], [0, 14, 0, 14, 59], [0, 21, 0, 21, 59], [0, 22, 0, 22, 59], [1, 5, 0, 5, 59], [1, 6, 0, 6, 59], [1, 13, 0, 13, 59], [1, 14, 0, 14, 59], [1, 21, 0, 21, 59], [1, 22, 0, 22, 59], [2, 5, 0, 5, 59], [2, 6, 0, 6, 59], [2, 13, 0, 13, 59], [2, 14, 0, 14, 59], [2, 21, 0, 21, 59], [2, 22, 0, 22, 59], [3, 5, 0, 5, 59], [3, 6, 0, 6, 59], [3, 13, 0, 13, 59], [3, 14, 0, 14, 59], [3, 21, 0, 21, 59], [3, 22, 0, 22, 59], [4, 5, 0, 5, 59], [4, 6, 0, 6, 59], [4, 13, 0, 13, 59], [4, 14, 0, 14, 59], [4, 21, 0, 21, 59], [4, 22, 0, 22, 59], [5, 5, 0, 5, 59], [5, 6, 0, 6, 59], [5, 13, 0, 13, 59], [5, 14, 0, 14, 59], [5, 21, 0, 21, 59], [5, 22, 0, 22, 59], [6, 5, 0, 5, 59], [6, 6, 0, 6, 59], [6, 13, 0, 13, 59], [6, 14, 0, 14, 59], [6, 21, 0, 21, 59], [6, 22, 0, 22, 59]]

app_list = combine_all_appointments(test)
formatted = format_appointments(app_list)
grouped = group_by_day(formatted)
返回

[[0, '4:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [1, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [2, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [3, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [4, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [5, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [6, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59']]

在我看来,您的输入是一个正在使用的小时分钟数列表,而插槽的概念是将连续的分钟数合并为一个单位。所以你应该用不同的连续小时数来测试你的转换函数。不确定您当前的浮点和str代码试图做什么。这解决了75%的问题,我得到的每个列表都像['0','4:00','5:59','5:00','5:59','6:59','13:00','13:59','14:00','14:59','21:00','21:59','22:59',],但我希望它像['0','4:00','5:59','13:00','14:59','21:00','22:59']请澄清您在列表中包含或排除时段的标准是什么?从您的示例中,不清楚需要包括哪些时段。['1','5:00','5:59','6:00','6:59','13:00','13:59','14:00','14:59','21:00','21:59','22:00','22:59']。在这个例子中,我在6:59和13:00之间有一个间隙。因此,我的第一个插槽将在5点开始,6点69分结束。第二个将在13点开始,14点59分结束,因为14点59分到21点之间有休息时间。基本上,如果有顺序中断。是的,在我的例子中有输入错误,我修正了。你的第二个假设是正确的,我想合并连续的假设。但我的最后一张单子应该是[0,4.00,6:59,13:00,14:59,21:00,22:59]好的,我的答案是你想要的吗?[0,4:00,4:59,5:00,5:59,6:00,6:59,13:59,14:59,21:00,21:59,22:59]。我已经说到这一点了,但是仍然出现了一些连续的值。好的,我在上面的示例中看不到任何连续的约会。哪一个是问题?是否可以将每天分成一个列表,输出将只包含0-6个列表,每个列表包含每天和约会。
[[0, '4:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [1, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [2, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [3, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [4, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [5, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59'], [6, '5:00', '6:59', '13:00', '14:59', '21:00', '22:59']]