解析具有多个相同键的词典列表

时间:2018-06-02 04:13:10

标签: python django dictionary

我从这个词典列表开始。

[{'allow_day_and_time': {'day': 'Monday', 'start': 9, 'end': 18}}, 
{'allow_day_and_time': {'day': 'Tuesday', 'start': 9, 'end': 18}}, 
{'allow_day_and_time': {'day': 'Wednesday', 'start': 9, 'end': 18}}, 
{'allow_day_and_time': {'day': 'Thursday', 'start': 9, 'end': 18}}, 
{'allow_day_and_time': {'day': 'Friday', 'start': 9, 'end': 18}}]

我申请了这个。

index = 0
    while index < len(availability_constraints):
        for key in availability_constraints[index]:
            print(availability_constraints[index][key])
        index += 1

结果

{'day': 'Monday', 'start': 9, 'end': 18}
{'day': 'Tuesday', 'start': 9, 'end': 18}
{'day': 'Wednesday', 'start': 9, 'end': 18}
{'day': 'Thursday', 'start': 9, 'end': 18}
{'day': 'Friday', 'start': 9, 'end': 18}

我可能会在返回的任何一天返回多个开始和结束值,例如星期一开始9,结束11,星期一开始13,结束18。

我所挣扎的是如何获得重新开始和结束的时间,并记住我可能会在同一天返回多个开始和结束时间。

任何指针都会有很大的帮助。

2 个答案:

答案 0 :(得分:1)

我认为您的完整数据可能看起来像这样:

[{'allow_day_and_time': {'day': 'Monday', 'start': 9, 'end': 18}}, 
{'allow_day_and_time': {'day': 'Tuesday', 'start': 9, 'end': 18}}, 
{'allow_day_and_time': {'day': 'Wednesday', 'start': 9, 'end': 18}}, 
{'allow_day_and_time': {'day': 'Thursday', 'start': 9, 'end': 18}}, 
{'allow_day_and_time': {'day': 'Friday', 'start': 9, 'end': 18}},
{'allow_day_and_time': {'day': 'Monday', 'start': 13, 'end': 21}}, 
{'allow_day_and_time': {'day': 'Tuesday', 'start': 13, 'end': 21}}, 
{'allow_day_and_time': {'day': 'Wednesday', 'start': 13, 'end': 21}}, 
{'allow_day_and_time': {'day': 'Thursday', 'start': 13, 'end': 21}}, 
{'allow_day_and_time': {'day': 'Friday', 'start': 13, 'end': 21}}]

由于您的数据是listdictionary个商品,因此只需在key括号内提供[]即可access the dictionary,或者,您可以检索使用dictionary.iteritems()

keyvalue

使用dictionary.iteritems()

进行迭代
In [  ]: index = 0
    ...: while index < len(availability_constraints):
    ...:     for key, value in availability_constraints[index].iteritems():
    ...:         print(key, value)
    ...:     index += 1
    ...: 
('allow_day_and_time', {'start': 9, 'end': 18, 'day': 'Monday'})
('allow_day_and_time', {'start': 9, 'end': 18, 'day': 'Tuesday'})
('allow_day_and_time', {'start': 9, 'end': 18, 'day': 'Wednesday'})
('allow_day_and_time', {'start': 9, 'end': 18, 'day': 'Thursday'})
('allow_day_and_time', {'start': 9, 'end': 18, 'day': 'Friday'})
('allow_day_and_time', {'start': 13, 'end': 21, 'day': 'Monday'})
('allow_day_and_time', {'start': 13, 'end': 21, 'day': 'Tuesday'})
('allow_day_and_time', {'start': 13, 'end': 21, 'day': 'Wednesday'})
('allow_day_and_time', {'start': 13, 'end': 21, 'day': 'Thursday'})
('allow_day_and_time', {'start': 13, 'end': 21, 'day': 'Friday'})

假设您正在查询Monday的开头,您可以使用if==运算符来检查列表中该特定元素的value是否正在引用到Monday

In [  ]: index = 0
    ...: while index < len(availability_constraints):
    ...:     for key, value in availability_constraints[index].iteritems():
    ...:         if value['day'] == 'Monday':
    ...:             print("Start: {:>4}, Finish: {:>4}".format(value['start'], value['end']))
    ...:     index += 1
    ...: 
Start:    9, Finish:   18
Start:   13, Finish:   21

使用[]进行迭代并提供硬编码的key

另一个更直接/硬编码,可能不是最好的编程实践,如下所示:

In [  ]: for a_day in availability_constraints:
    ...:     if a_day['allow_day_and_time']['day'] == 'Monday':
    ...:         print("Start: {:>4}, Finish: {:>4}".format(
    ...:          a_day['allow_day_and_time']['start'],
    ...:          a_day['allow_day_and_time']['end']))
    ...:         
    ...:         
Start:    9, Finish:   18
Start:   13, Finish:   21

我们还可以引入间接方式以便更好地进行维护:

In [  ]: key = 'allow_day_and_time'
    ...: target_day = 'Monday'
    ...: begin = 'start'
    ...: finish = 'end'
    ...: for a_day in availability_constraints:
    ...:     if a_day[key]['day'] == target_day:
    ...:         print("Start: {:>4}, Finish: {:>4}".format(
    ...:          a_day[key][begin],
    ...:          a_day[key][finish]))
    ...:         
    ...:         
Start:    9, Finish:   18
Start:   13, Finish:   21

结论:pythonic方式

检查此link是否为pythonic string formatting

In [  ]: for a_day in availability_constraints:
    ...:     for key, value in a_day.iteritems():
    ...:         if value['day'] == 'Monday':
    ...:             print("Start: {:>4}, Finish: {:>4}".format(value['start'], value['end']))
Start:    9, Finish:   18
Start:   13, Finish:   21

哪种编程风格最好?好吧,我自己很自以为是,所以我认为这取决于你的偏好,除非你想要优化这个程序:D

答案 1 :(得分:0)

您可以使用itertools.groupby查找与常用开始值和结束值相关联的所有日期:

import itertools
def time_key(x:dict):
  _d = x['allow_day_and_time']
  return _d['start'], _d['end']

d = [{'allow_day_and_time': {'day': 'Monday', 'start': 9, 'end': 18}}, {'allow_day_and_time': {'day': 'Tuesday', 'start': 9, 'end': 18}}, {'allow_day_and_time': {'day': 'Wednesday', 'start': 9, 'end': 18}}, {'allow_day_and_time': {'day': 'Thursday', 'start': 9, 'end': 18}}, {'allow_day_and_time': {'day': 'Friday', 'start': 9, 'end': 18}}, {'allow_day_and_time': {'day': 'Monday', 'start': 13, 'end': 21}}, {'allow_day_and_time': {'day': 'Tuesday', 'start': 13, 'end': 21}}, {'allow_day_and_time': {'day': 'Wednesday', 'start': 13, 'end': 21}}, {'allow_day_and_time': {'day': 'Thursday', 'start': 13, 'end': 21}}, {'allow_day_and_time': {'day': 'Friday', 'start': 13, 'end': 21}}]
new_d = [{'start':_s, 'end':_e, 'days':[i['allow_day_and_time']['day'] for i in b]} for [_s, _e], b in itertools.groupby(sorted(d, key=time_key), key=time_key)]

输出:

[{'start': 9, 'end': 18, 'days': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']}, {'start': 13, 'end': 21, 'days': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']}]