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 3691472
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:09:06+00:00 2026-05-19T04:09:06+00:00

I just started using Python as hobby, so forgive me if this is a

  • 0

I just started using Python as hobby, so forgive me if this is a dumb question.

I recently installed a power meter on my computer, and I’m using it to track its power consumption. It gives me real-time minute readings exported to a csv file.

I have parsed and read the file, and now I have a dictionary where the key is a timestamp, stored as a struct_time from the time library. Currently, the dictionary has readings which were taken every minute. I’d like to create a new dictionary whose keys are hour timestamps, and whose values are the sum of the individual minute readings.

I thought about looping through the dictionary with a counter mod 60, but I’m told that isn’t very Pythonic. Also, there’s no guarantee that there exists 60 readings for every hour. How should I do this?

  • 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-19T04:09:07+00:00Added an answer on May 19, 2026 at 4:09 am

    First, I’d suggest using the datetime.datetime class instead of time.time_struct, since the latter is really just a weak wrapper around a 9-tuple but the former is a fully-featured class with operator overloads etc. Next, you can use a collections.defaultdict to easily construct your desired total readings:

    # power_reading is the data type corresponding to one set of power readings
    # (it could be a simple float, or a tuple, or a class if you want; just make
    # sure that you can add them together)
    hourly_readings = collections.defaultdict(power_reading)
    minutely_readings = { ... };  # dict of time.struct_time ==> power_reading
    
    for timestamp, reading in minutely_readings.iteritems():
        hour = datetime.datetime(timestamp.tm_year, timestamp.tm_mon,
                                 timestamp.tm_mday, timestamp.tm_hour);
        hourly_readings[hour] += reading
    

    If power_reading is a tuple, you can’t use += (since that concatenates the tuples instead of doing an element-wise addition), so you’ll have to do something like this instead:

    hourly_readings[hour] = tuple(map(sum, zip(hourly_readings[hour], reading)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just started using rpy2 with Python. I've installed it and am able to
I've recently been learning python and I just started playing with networking using python's
I have programmed in Python for a while, and just recently started using Ruby
I've just started using Python and I was thinking about which notation I should
I've just started using Eclipse for Python development since we can make use of
I just started with Google App Engine using python and I was following a
I have made an online gallery using Python and Django. I've just started to
I have started using Python for web development recently, it's kinda cool; I have
All, I've just started using Python (v 2.7.1) and one of my first programs
Have just started using Google Chrome , and noticed in parts of our site,

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.