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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:38:32+00:00 2026-06-14T21:38:32+00:00

I have data in a 3d dictionary as: movieid, date,customer_id,views 0, (2011,12,22), 0, 22

  • 0

I have data in a 3d dictionary as:

 movieid, date,customer_id,views
 0, (2011,12,22), 0, 22
 0, (2011,12,22), 1, 2
 0, (2011,12,22), 2, 12
 .....
 0, (2011,12,22), 7, 2
 0, (2011,12,23), 0, 123

..
so basically the data represents how many times a movie has been watched each day.. by each customer (there are just 8 customers)..

Now, I want to calculate.. on average how many times a movie has been watched by each customer.

So basically

    movie_id,customer_id, avg_views
     0, 0, 33.2
     0, 1 , 22.3

  and so on

What is the pythonic way to solve this.

Thakns

Edit:

 data = defaultdict(lambda : defaultdict(dict))
 date = datetime.datetime(2011,1,22)
 data[0][date][0] = 22
 print data
defaultdict(<function <lambda> at 0x00000000022F7CF8>, 
 {0: defaultdict(<type 'dict'>, 
 {datetime.datetime(2011, 1, 22, 0, 0): {0: 22}}))

Suppose there are just 2 customers, 1 movie and 2 days worth of data

 movie_id, date, customer_id,views
 0 , 2011,1,22,0,22
 0 , 2011,1,22,1,23
 0 , 2011,1,23,0,44

note: The customer 1 didnt watched a movie id 0 on 23rd jan
Now the answer would

 movie_id,customer_id,avg_views
  0   , 0 ,    (22+44)/2
  0,    1,      (23)/1
  • 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-06-14T21:38:33+00:00Added an answer on June 14, 2026 at 9:38 pm

    sum makes this easy. In my original version I used dict.keys() a lot, but iterating over a dictionary gives you the keys by default.

    This function calculates a single line of the result:

    def average_daily_views(movie_id, customer_id, data):
        daily_values = [data[movie_id][date][customer_id] for date in data[movie_id]]
        return sum(daily_values)/len(daily_values)
    

    Then you can just loop it to get whatever form you want. Maybe:

    def get_averages(data):
        result = [average_daily_views(movie, customer, data) for customer in 
                  data[movie] for movie in data]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data in a python dictionary that I'd like to store in a
I have some data in this form (dictionary): Value0 Text1 Value1 Text2 Value2 Text3
I have a string data which I need to parse into a dictionary object.
Say that i have a generic dictionary with data like this (I hope the
I have a website that holds a dictionary of data sets. When a user
I have a file (dictionary.txt) with data field entries as follows - ABC This
I have data from travel diaries which has been read in from a csv
I have a data dictionary which is as follows: Key Value 1 ron k
I have a Dictionary data base and I want to return an element from
Based on couple of Api Calls I have constructed the following data structure (Dictionary).

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.