python 中二维列表的逐元素操作

Element-wise manipulation of a two dimensional list in python

我正在编写一个脚本来寻找三个人的会面时间。我设法以二进制格式获取他们的 Free/Busy 状态编码,其中 0 表示空闲,1 表示在接下来的三天内以 30 分钟的增量忙碌。我按天将他们的状态分组为字典格式,如下所示。

print(date_schedule)
{'Monday, 2020-02-03': ['000000000000000000101101001110110000000000000000',
  '000000000000000000001111011100001100000000000000',
  '000000000000000011110100011000110000000000000000'],
 'Tuesday, 2020-02-04': ['000000000000000000100010000000000000000000000000',
  '000000000000000000001111001000110000000000000000',
  '000000000000000011111000111100101000000000000000'],
 'Wednesday, 2020-02-05': ['000000000000000000111000000000000000000000000000',
  '000000000000000001001100110000000000000000000000',
  '000000000000000000111100000001001000000000000000']}

目标: 将那些 0 转换为三十分钟间隔的块。

For Example: 00:00----00:30
             00:30----01:00
                   ...
             23:30----24:00

尝试次数:

#Separate the code into a two dimensional list
schedule = date_free.values()

#Append the block to a new list.
free = []
for value in schedule:
   for v in value:
       for idx, time in enumerate(v):
           if time == '0':
                idx = idx/2
                end = idx + 0.5
                #5 slots, and two decimals
                idx = '{:05.2f}'.format(idx).replace('.50','.30').replace('.',':')
                end =  '{:05.2f}'.format(end).replace('.50','.30').replace('.',':')
                free.append((idx + '----' + end))

问题: free有372个元素,不知道怎么变成二维列表结构之前在schedule(因为每个v的0个数不同)。有没有办法不创建新列表,而是直接将上述逻辑逐元素应用于 schedule?

奖金问题:我还没有到达那里,但我的下一个目标是找到每天这 30 个时间块的交集,如 random 下面的例子。如果您有任何建议,请告诉我

print(date_time_final)
{'Monday, 2020-02-03': ['08:00----08:30','09:30----10:00','12:00----12:30'],
'Tuesday, 2020-02-04' : ['09:00----09:30','10:30----11:00','13:00----13:30','14:00----14:30']
'Wednesday, 2020-02-05' : ['07:00----07:30','14:30----15:00','15:00----15:30','19:00----19:30']}

提前感谢您的帮助!

这就是您要找的东西吗?

schedule = {'Monday, 2020-02-03': ['000000000000000000101101001110110000000000000000',
  '000000000000000000001111011100001100000000000000',
  '000000000000000011110100011000110000000000000000'],
 'Tuesday, 2020-02-04': ['000000000000000000100010000000000000000000000000',
  '000000000000000000001111001000110000000000000000',
  '000000000000000011111000111100101000000000000000'],
 'Wednesday, 2020-02-05': ['000000000000000000111000000000000000000000000000',
  '000000000000000001001100110000000000000000000000',
  '000000000000000000111100000001001000000000000000']}

combined = {}
for value in schedule:
    day = {}
    for v in schedule[value]:
        for idx, time in enumerate(v):
            idx = idx/2
            end = idx + 0.5
            #5 slots, and two decimals
            idx = '{:05.2f}'.format(idx).replace('.50','.30').replace('.',':')
            end =  '{:05.2f}'.format(end).replace('.50','.30').replace('.',':')

            if time == '0':
                try: #Only assigns "True" if value does not yet exist and is not already False
                    if day[idx + '----' + end] == False:
                        pass
                    else:
                        day[idx + '----' + end] = True
                except:
                    day[idx + '----' + end] = True
            elif time == '1':
                day[idx + '----' + end] = False
    combined[value] = day

for day in combined:
    print(day)
    for time_slot in combined[day]:
        print("Time slot %s is free = %s" % (time_slot, str(combined[day][time_slot])))

我没有使用列表,而是使用字典格式按日期和时间段排序,维护记录的每个时间段,但给它们一个布尔值以确定它是否空闲。 (真 == 空闲,假 == 忙) 这样你就可以随心所欲地处理输出。