我有一个包含日期属性的对象列表(django模型)。日/月/年我需要按月和年分组,所以如果我有:
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()
...
...
答案 0 :(得分:1)
您实际上可以通过直接注释查询集来按月和按年分组:
--sysctl net.ipv6.conf.all.disable_ipv6=0
https://docs.djangoproject.com/en/2.0/ref/models/database-functions/#datefield-truncation
答案 1 :(得分:0)
您可以使用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)
答案 2 :(得分:0)
@ 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']}}
之类的词典来根据年份和时间来组织日期。一个月。
>>> s = {[1, 2, 3], [2, 3, 4], [2, 3, 4]}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>>
>>> s = {(1, 2, 3), (2, 3, 4), (2, 3, 4)}
>>> s
set([(2, 3, 4), (1, 2, 3)])
>>>
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"
]
]
"""