Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6782991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:48:38+00:00 2026-05-26T16:48:38+00:00

Hopefully I can explain what I am trying to accomplish. I have no problem

  • 0

Hopefully I can explain what I am trying to accomplish. I have no problem achieving my result, but I know that this is probably not the best way to do it.

I have a table with some entries by date. I am trying to take those entries from the current month, and arrange them into a list by week, and then sum a value from the table for each day of the week. The end result would look something like this:

{44: {4: Decimal('2.80'), 5: Decimal('6.30')}, 45: {1: Decimal('8.90'), 2: Decimal('10.60')}}

I have a solution. But, I know this is not the best way to do it. Any ideas about how to make this better?

#Queries the database and returns time objects that have fields 'hours' and 'date'
time = self.month_time()

r = {}
for t in time:
    #Get the week of the year number
    week = t.date.isocalendar()[1]

    #Get the day of the week number
    day = t.date.isoweekday()

    if week not in r:
        r.update({week:{}})
    if day not in r[week]:
        r[week][day] = 0

    r[week][day] = r[week][day] + t.hours
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:48:39+00:00Added an answer on May 26, 2026 at 4:48 pm

    I think you are probably looking for the defaultdict. A defaultdict is just like a dictionary, except when a KeyError would be thrown with dict, the factory function given upon initialization is used to create an initial value.

    In your case, you’ll need a defaultdict for days nested inside one for weeks. I think this will work for you:

    from collections import defaultdict
    
    r = defaultdict(lambda: defaultdict(int))
    for t in time:
        week = t.date.isocalendar()[1]
        day = t.date.isoweekday()
        r[week][day] += t.hours
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully I can explain this to where it make sense, but I'm trying to
Hopefully I can explain this well enough, but please let me know if more
Sorry for the unhelpful title, but hopefully I can explain this well enough. Lets
Hopefully I can explain this clearly. I'm looking to have a two sliders, and
Hopefully I can explain this correctly. I have a table of line orders (each
First off I'm not sure that's the proper title for this question but hopefully
Hey everyone. Hopefully I can explain this correctly. I have some URL's which I
Long title but hopefully I can explain what I am after. I have read
I'm not too sure on how to explain this but hopefully you get what
This is a weird one, but hopefully someone can give me an idea here.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.