python中二维列表的元素明智操作

时间:2020-02-04 04:19:47

标签: string multidimensional-array python-3.6 win32com

我正在编写一个脚本来查找三个人的开会时间。我设法以二进制格式获取其忙/闲状态编码,在接下来的三天内,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 转换为每30分钟间隔一次。

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

问题:免费有372个元素,我不知道如何像在计划中那样使其成为二维列表结构(因为每个v的0数不同)。有没有办法不创建新列表,而是将上述逻辑元素直接应用于时间表

奖金问题:我还没有到达那里,但是我的下一个目标是找到每天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']}

预先感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您正在寻找这样的东西吗?

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

我使用字典格式而不是列表来按天和按时隙排序,将每个时隙保持在记录中,但是给它们一个布尔值以确定其是否空闲。 (正确==空闲,错误==忙) 这样,您可以对输出执行任何操作。