Django Python按月将列表拆分为各种列表
我有一个对象列表(django模型),其中包括date atribute。天/月/年,我需要按月和年将其分成若干组,因此如果我有:Django Python按月将列表拆分为各种列表,django,python-3.x,django-models,Django,Python 3.x,Django Models,我有一个对象列表(django模型),其中包括date atribute。天/月/年,我需要按月和年将其分成若干组,因此如果我有: list_dates = ['1-10-2018','4-10-2018','1-11-2018', '6-10-2018', '3-10-2018', '6-11-2018', '9-11-2018'] 这实际上是: list_objects = [a,b,c,d,e,f,g...z] 编辑:每个对象(模型)都有一个date=model
list_dates = ['1-10-2018','4-10-2018','1-11-2018',
'6-10-2018', '3-10-2018', '6-11-2018', '9-11-2018']
这实际上是:
list_objects = [a,b,c,d,e,f,g...z]
编辑:每个对象(模型)都有一个date=models.Datetime()
,我需要选择和分组具有相同月份和年份的对象
把它变成
splitted_list = {[a,b,e],[c,d,e]}# same month, same year grouped together
list_grouped = {['1-10-2018','4-10-2018','1-10-2018','6-10-2018'],
['3-11-2018', '6-11-2018', '9-11-2018']}
我还没有找到一个简单可行的方法来做到这一点
希望有人知道怎么做。我已经试了好几天了
'class my_model<(models.Model):
owner = models.ForeignKey('User', on_delete=models.CASCADE, related_name="model_prods")
...
...
class list_v2(models.Model):
list_owner= models.ForeignKey(my_model, related_name='date_list', on_delete=models.CASCADE)
date = models.DateField()
...
...
'class my_model您可以使用dict
对象来跟踪作为键的月份和年份的组合,以及它们在结果列表中的相应位置作为这些键的值
示例:
list_objects = [a, b, c...]
month_year_dict = {}
result = []
for obj in list_objects:
dt = obj.date_field_name
day, month, year = dt.day, dt.month, dt.year
key = month + "-" + year
if month_year_dict.get(key, None):
result[month_year_dict[key]].append(obj)
else:
result.append([obj])
month_year_dict[key] = len(result) - 1
print(result)
@Zealot91,您可以尝试以下代码示例
注意:在Python中不能使用像{[1,2,3],[2,3,4],[2,3,4]}
这样的语句,但是{(1,2,3)、(2,3,4)、(2,3,4)}
是可以的
因此,我建议您使用/创建[[1,2,3],[2,3,4],[2,3,4]
类型列表作为最终结果,或者您可以使用类似{'2018':{'10':['12-10-2018','12-10-2018'],'11':['12-11-2018','9-11-2018']}}这样的词汇来根据年份和月份组织日期
»示例2
通过直接注释查询集,您可以按月份和年份进行分组,如下所示:
filtered_queryset = queryset.annotate(month=TruncMonth('date'), year=TruncYear('date'))
filtered_queryset.values_list('month', 'year')
看看itertools.groupby
@Selcuk我的问题是我有一个对象列表,product.requireds.all().order\u by('date'):
或p=product.objects.filter(owner=request.user)
并且对象有一个date=models.Datetime(),所以我认为itertools.groupby没有用处。或者我不明白它是如何工作的,希望是后者,但就我所看到的,它在列表的元组上工作(如果我的语义正确的话),你必须先对你的数据结构进行一些调整。例如,尝试使用[(int(i)代表x中的i.split('-'))代表列表中的x]
。@Selcuk,在Python中不能使用{[1,2,3],[2,3,4],[2,3,4]}
这样的语句,但是{[1,2,3],[2,3,4]}
是可以的。因此,使用/create[[1,2,3],[2,3,4],[2,3,4]
类型列表作为最终结果,或者您可以使用类似于{'2018':{'10':['12-10-2018','12-10-2018','11':['12-11-2018','9-11-2018']}}的词汇来根据年和月组织日期。这是原始sql查询吗?不,这是Django内置的queryset函数
import json
# List of dates
list_dates = [
'1-10-2018','4-10-2018','1-11-2018',
'6-10-2018', '3-10-2018', '6-11-2018',
'9-11-2018'
]
d = {}
for date in list_dates:
day, month, year = date.split('-')
if year in d:
if month in d[year]:
d[year][month].append(date)
else:
d[year][month] = [date]
else:
d[year] = {month: [date]}
# Pretty printing d (Example 1.1: dictionary)
print(json.dumps(d, indent=4))
"""
{
"2018": {
"11": [
"1-11-2018",
"6-11-2018",
"9-11-2018"
],
"10": [
"1-10-2018",
"4-10-2018",
"6-10-2018",
"3-10-2018"
]
}
}
"""
# Creating and pretty printing list_grouped (Example 1.2: list of lists)
list_grouped = [months_list for year in d.values() for months_list in year.values()]
print(json.dumps(list_grouped, indent=4))
"""
[
[
"1-11-2018",
"6-11-2018",
"9-11-2018"
],
[
"1-10-2018",
"4-10-2018",
"6-10-2018",
"3-10-2018"
]
]
"""
# List of dates
import json
list_dates = [
'1-10-2018','28-01-2017', '4-10-2018','1-11-2018',
'6-10-2018', '3-10-2018', '6-12-2016', '6-11-2018',
'9-11-2018', '15-11-2016', '14-05-1992', '03-11-2017',
'1-10-2018','25-01-2017', '4-11-2017','1-11-2016',
'6-10-2018', '3-11-2016', '6-12-2017', '6-10-2013',
'9-12-2014', '15-10-2013', '20-05-1992', '03-12-2017',
'19-12-2014', '15-10-2013', '20-05-1992', '03-12-2017'
]
d = {}
for date in list_dates:
day, month, year = date.split('-')
if year in d:
if month in d[year]:
d[year][month].append(date)
else:
d[year][month] = [date]
else:
d[year] = {month: [date]}
# Pretty printing d (Example 2.1: dictionary)
print(json.dumps(d, indent=4))
"""
{
"1992": {
"05": [
"14-05-1992",
"20-05-1992",
"20-05-1992"
]
},
"2018": {
"11": [
"1-11-2018",
"6-11-2018",
"9-11-2018"
],
"10": [
"1-10-2018",
"4-10-2018",
"6-10-2018",
"3-10-2018",
"1-10-2018",
"6-10-2018"
]
},
"2014": {
"12": [
"9-12-2014",
"19-12-2014"
]
},
"2017": {
"11": [
"03-11-2017",
"4-11-2017"
],
"12": [
"6-12-2017",
"03-12-2017",
"03-12-2017"
],
"01": [
"28-01-2017",
"25-01-2017"
]
},
"2016": {
"11": [
"15-11-2016",
"1-11-2016",
"3-11-2016"
],
"12": [
"6-12-2016"
]
},
"2013": {
"10": [
"6-10-2013",
"15-10-2013",
"15-10-2013"
]
}
}
"""
# Creating and pretty printing list_grouped (Example 2.2: list of lists)
list_grouped = [months_list for year in d.values() for months_list in year.values()]
print(json.dumps(list_grouped, indent=4))
"""
[
[
"14-05-1992",
"20-05-1992",
"20-05-1992"
],
[
"1-11-2018",
"6-11-2018",
"9-11-2018"
],
[
"1-10-2018",
"4-10-2018",
"6-10-2018",
"3-10-2018",
"1-10-2018",
"6-10-2018"
],
[
"9-12-2014",
"19-12-2014"
],
[
"03-11-2017",
"4-11-2017"
],
[
"6-12-2017",
"03-12-2017",
"03-12-2017"
],
[
"28-01-2017",
"25-01-2017"
],
[
"15-11-2016",
"1-11-2016",
"3-11-2016"
],
[
"6-12-2016"
],
[
"6-10-2013",
"15-10-2013",
"15-10-2013"
]
]
"""
filtered_queryset = queryset.annotate(month=TruncMonth('date'), year=TruncYear('date'))
filtered_queryset.values_list('month', 'year')