python 分组不工作
python group by not working
我有一个字典列表,我想按字符串类型的字段对它们进行分组
recs = [
{'r_date': datetime.date(2014, 1, 1), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 1), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 1), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 1), 'r_time_start': datetime.time(12, 0), 'r_item1_name': 'Student2', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 2), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 2), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 2), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 2), 'r_time_start': datetime.time(12, 0), 'r_item1_name': 'Student2', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 3), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 3), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 3), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 3), 'r_time_start': datetime.time(12, 0), 'r_item1_name': 'Student2', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 4), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 4), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 4), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 4), 'r_time_start': datetime.time(14, 0), 'r_item1_name': 'Namestnik', 'r_time_end': datetime.time(22, 0)},
{'r_date': datetime.date(2014, 1, 5), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 5), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 5), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 5), 'r_time_start': datetime.time(14, 0), 'r_item1_name': 'Namestnik', 'r_time_end': datetime.time(22, 0)},
{'r_date': datetime.date(2014, 1, 6), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 6), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 6), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 6), 'r_time_start': datetime.time(14, 0), 'r_item1_name': 'Namestnik', 'r_time_end': datetime.time(22, 0)}]
我想获取按 r_item1_name
分组的字典列表
grouped = [{"r_item1_name": "Sef","AllData" : [{record_of_sef},{another_record_of_sef},{another_record_of_sef}] }]
for key, items in itertools.groupby(recs, operator.itemgetter('r_item1_name')):
grupirani1.append({"r_item1_name": key, "AllData": list(items)})
如果我按日期类型 (r_date) 对代码组进行分组,则按预期进行。
知道为什么吗?
itertools.groupby
要求输入数据已经按用于分组的键排序。只有具有相同键的连续值才会被分组,因此如果数据未排序,您将为具有相同值的每个块获得单独的组。
我重新格式化了您问题中的数据,以便可以查看各个项目。现在应该清楚它是按 r_date
排序的,因此分组将在该字段上起作用,但您需要对其进行排序以在任何其他字段上分组。
我有一个字典列表,我想按字符串类型的字段对它们进行分组
recs = [
{'r_date': datetime.date(2014, 1, 1), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 1), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 1), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 1), 'r_time_start': datetime.time(12, 0), 'r_item1_name': 'Student2', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 2), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 2), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 2), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 2), 'r_time_start': datetime.time(12, 0), 'r_item1_name': 'Student2', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 3), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 3), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 3), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 3), 'r_time_start': datetime.time(12, 0), 'r_item1_name': 'Student2', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 4), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 4), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 4), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 4), 'r_time_start': datetime.time(14, 0), 'r_item1_name': 'Namestnik', 'r_time_end': datetime.time(22, 0)},
{'r_date': datetime.date(2014, 1, 5), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 5), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 5), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 5), 'r_time_start': datetime.time(14, 0), 'r_item1_name': 'Namestnik', 'r_time_end': datetime.time(22, 0)},
{'r_date': datetime.date(2014, 1, 6), 'r_time_start': datetime.time(7, 0), 'r_item1_name': 'Sef', 'r_time_end': datetime.time(12, 0)},
{'r_date': datetime.date(2014, 1, 6), 'r_time_start': datetime.time(8, 0), 'r_item1_name': 'Moj\xc4\x8dca', 'r_time_end': datetime.time(15, 0)},
{'r_date': datetime.date(2014, 1, 6), 'r_time_start': datetime.time(9, 0), 'r_item1_name': 'Student1', 'r_time_end': datetime.time(19, 0)},
{'r_date': datetime.date(2014, 1, 6), 'r_time_start': datetime.time(14, 0), 'r_item1_name': 'Namestnik', 'r_time_end': datetime.time(22, 0)}]
我想获取按 r_item1_name
分组的字典列表grouped = [{"r_item1_name": "Sef","AllData" : [{record_of_sef},{another_record_of_sef},{another_record_of_sef}] }]
for key, items in itertools.groupby(recs, operator.itemgetter('r_item1_name')):
grupirani1.append({"r_item1_name": key, "AllData": list(items)})
如果我按日期类型 (r_date) 对代码组进行分组,则按预期进行。
知道为什么吗?
itertools.groupby
要求输入数据已经按用于分组的键排序。只有具有相同键的连续值才会被分组,因此如果数据未排序,您将为具有相同值的每个块获得单独的组。
我重新格式化了您问题中的数据,以便可以查看各个项目。现在应该清楚它是按 r_date
排序的,因此分组将在该字段上起作用,但您需要对其进行排序以在任何其他字段上分组。