在python中从列表中提取槽

2024-09-25 02:26:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下格式的列表,我需要传递给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,时隙2是13:00到14:59,slot3是21:00到22:59。在

我试图将清单简化如下。在

^{pr2}$

从本质上讲,每天的小时槽提取并组合成一个列表,因此最终输出只有第0-6天的7个列表。在

另外请注意,上面的格式可能会改变,对于任何给定的一天,可能只有1个时隙或者可能没有一个时隙,因此每天的时隙可能在0-3之间变化。在

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

^{3}$

Tags: api元素列表格式例子时隙小时本质
2条回答

我还假设您希望: (a) 合并连续约会(即在前一个约会结束后立即开始的约会) (b) 按照上述方式格式化这些文件 (c) 把这些按天排列成扁平的列表。在

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

(a)创建一个helper函数,将日、小时和分钟变量转换为分钟:

def get_minute_val(day_val,hour_val,min_val):
    return (24*60*day_val)+(60*hour_val)+min_val

(b)生成一个接受两个约会的函数,如果两个约会是连续的,则将它们合并为一个;如果不是连续的,则返回未合并的约会

^{pr2}$

(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']]

你可以这样做:

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()

相关问题 更多 >