简体   繁体   English

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

[英]Extracting slots from list of lists in python

I have list of lists in following format which I need to pass to an api.我有以下格式的列表列表,我需要将其传递给 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] 
]

In each list first element represents the day, and following elements represents from hour & mins to hour & mins.在每个列表中,第一个元素代表日期,随后的元素代表从小时和分钟到小时和分钟。 From the example above, for day 0, slot1 is 04:00 to 6:59, slot 2 is 13:00 to 14:59, and slot3 is 21:00 to 22:59.从上面的例子来看,对于第 0 天, slot1是 04:00 到6: 59, slot 2 是13:00 到 14:59, slot3 是21:00 到 22:59。

I am trying to simplify the lists to as follows.我正在尝试将列表简化为如下。

[0, 04:00, 6:59, 13:00, 14:59, 21:00, 22:59]....

Essentially Extracting and combining the hours slots for each day into a single list, hence final output would have only 7 lists from day 0-6.本质上将每天的时间段提取并组合到一个列表中,因此最终输出将只有 0-6 天的 7 个列表。

Also notice above format could change, for any given day there might only be 1 slot or might not have a slot, so the slots could vary between 0-3 for each day.另请注意,上述格式可能会发生变化,对于任何给定的一天,可能只有 1 个插槽或可能没有插槽,因此每天的插槽可能在 0-3 之间变化。

So far I manage to join the hours and mins as follows `到目前为止,我设法按如下方式加入小时和分钟`

 start = float(str(from_hr) + str('.')+ str(from_min))
 end =   float(str(to_hr) + str('.')+ str(to_min))`

I also assume that you wish to: (a) Combine consecutive appointments (ie one starting immediately after the previous one ends) (b) Format these in the manner shown above (c) Group these by day in flattened lists.我还假设您希望: (a) 合并连续约会(即在前一个约会结束后立即开始约会) (b) 以上述方式格式化这些约会 (c) 在扁平列表中按天分组。

If so, you can solve this problem using the following approach:如果是这样,您可以使用以下方法解决此问题:

(a) Make a helper function that transforms your day, hour and minute variables into minutes: (a) 制作一个辅助函数,将您的天、小时和分钟变量转换为分钟:

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

(b) Make a function that takes two appointments, and either combines them to one if they are consecutive, or returns them uncombined if they are not (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) Iteratively call this on every appointment in the list, comparing vs. the most recently added appointment. (c) 在列表中的每个约会上迭代调用它,与最近添加的约会进行比较。 I have a slightly hacky method for dealing with the first appointment.我有一个处理第一次约会的有点老套的方法。

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) Make a function that does the formatting you want (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) and a separate one to group appointments by days, and flatten by day. (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]

Testing this on your input:在您的输入上测试:

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)

returns返回

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

You can do something like this:你可以这样做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM