Matheus, I put a code where I create a dictionary with the data that you passed it, and I create 2 methods: one to add the exact week of the year in each object of the user list, another to organize the output table , to be as close as possible to what you want to put there in your example.
To identify the week, I used the .isocalendar () attribute, which returns a tuple containing the calendar of the week.
import datetime
user_list = [
{
"name": "Talita",
"count": 4,
"date": datetime.date(year=2017, month=3, day=6),
},
{
"name": "Filho",
"count": 8,
"date": datetime.date(year=2017, month=3, day=7),
},
{
"name": "Jao",
"count": 10,
"date": datetime.date(year=2017, month=3, day=7),
},
{
"name": "Otavio",
"count": 6,
"date": datetime.date(year=2017, month=3, day=7),
},
{
"name": "Gabriel",
"count": 2,
"date": datetime.date(year=2017, month=3, day=8),
},
{
"name": "Guilherme",
"count": 1,
"date": datetime.date(year=2017, month=3, day=13),
},
]
def get_week(user_list):
for u in user_list:
week = u['date'].isocalendar()[1]
u['week'] = week
return user_list
def group_by_week(user_list):
actual_week = 0
week_group = {}
for u in user_list:
if actual_week != u['week']:
week_group[u['week']] = []
week_group[u['week']].append(u)
actual_week = u['week']
else:
week_group[actual_week].append(u)
return week_group
# Adicionando semana em cada objeto
print("~> Adicionando semana em cada objeto e printando")
user_list = get_week(user_list)
for t in user_list:
print("Name: %s, week: %s" % (t['name'], t['week']))
print("\n~> Organizando tabela de saida")
# Organizando tabela
week_group = group_by_week(user_list)
count_week = 0
for key in week_group:
if count_week < key:
count_week += 1
print("# Semana %s" % count_week)
for user in week_group[key]:
print("Name: %s, count: %s, date: %s" % (user['name'], user['count'], user['date']))
Result
~> Adicionando semana em cada objeto e printando
Name: Talita, week: 10
Name: Filho, week: 10
Name: Jao, week: 10
Name: Otavio, week: 10
Name: Gabriel, week: 10
Name: Guilherme, week: 11
~> Organizando tabela de saida
# Semana 1
Name: Talita, count: 4, date: 2017-03-06
Name: Filho, count: 8, date: 2017-03-07
Name: Jao, count: 10, date: 2017-03-07
Name: Otavio, count: 6, date: 2017-03-07
Name: Gabriel, count: 2, date: 2017-03-08
# Semana 2
Name: Guilherme, count: 1, date: 2017-03-13