-2

i have a json file that looks like below. In the result dictionary, there are keys which is date and each key has another dictionaries that have number key. These numbers means time. What I have to do is gather same number keys in different dates (ex: 2017-01-02[4]['buy']+ 2017-01-01[4]['buy']) and sum it, and get the average of buy or sold counts. I would like to know if there is simple and fast way to do this.

{
  "result": {
      "2017-01-02": {               
            "4": {
                "buy": [
                    2
                ],
                "sold": 4
            },
            "5": {
                "buy": [
                    1
                ],
                "sold": 4
            },
            "6": {
                "buy": [
                    67
                ],
                "sold": 54  
            }
        },
         "2017-01-01": {                
            "4": {
                "buy": [
                    44
                ],
                "sold": 8   
            },
            "5": {
                "buy": [
                    6
                ],
                "sold": 14
            },
            "6": {
                "buy": [
                    4
                ],
                "sold": 67
            }
        }
    }
}
ruth
  • 109
  • 2
  • 11

1 Answers1

0

I couldn't come up with a "simple and fast way" to do it, but this procedural way may work for your needs. The tricky part seems to be that some values in the dictionary are in list format (surrounded by brackets []) and others are single integers.

So to manage this, we can check the format of the item values, and use either sum() for the list values, or pythons increment notation (+=) for integers.

I've included other comments in the code.

import json

with open('data.json', 'r') as f:
    data = json.load(f)  # load json file contents

buy_total = 0
sold_total = 0
buy_count = sold_count = 0. # include `.` for average calculation as decimal

for date, d in data['result'].iteritems():
    for time, v in d.iteritems():
        if isinstance(v['buy'], list):  # if value is in brackets `[]`
            buy_count += len(v['buy'])
            buy_total += sum(v['buy'])
        elif isinstance(v['buy'], int): # if item is a single integer
            buy_count += 1
            sold_total += v['buy']
        if isinstance(v['sold'], list): # same as previous if
            sold_count += len(v['sold'])
            buy_total += sum(v['sold'])
        elif isinstance(v['sold'], int): # same as previous elif
            sold_count += 1 
            sold_total += v['sold']

    # optional print formatting
    # display this date's totals using float format https://stackoverflow.com/questions/455612/limiting-floats-to-two-decimal-points
    #   and string formatting mini-language https://docs.python.org/2/library/string.html#format-specification-mini-language 
    print '{dt}:\n\tbuy total: {bt}\n\tsold total: {st}\n\tbuy average: {b_avg:.2f}\n\tsold average: {s_avg:.2f}\n'.\
            format(dt=date, bt=buy_total, st=sold_total, b_avg=buy_total/buy_count, s_avg=sold_total/sold_count)

    # reset totals for next calculation
    buy_total = 0
    sold_total = 0
    buy_count = sold_count = 0.

output:

2017-01-01:
    buy total: 54
    sold total: 89
    buy average: 18.00
    sold average: 29.67

2017-01-02:
    buy total: 70
    sold total: 62
    buy average: 23.33
    sold average: 20.67

Note: although the calculations seem correct, please check them again to ensure accuracy.

Hope this helps.