0

I have the following list:

DATA

data_set = [(2, 2021, '2300'),(1, 2021, '2500'),(12, 2020, '2400'),(11, 2020, '1500')]

I want to format this list into the following one,

EXPECTED OUTPUT

    [
        {
            "2021-01-01T00": [
                {
                    "2021-02-01T00:00:00": 2300
                },
                {
                    "2021-01-01T00:00:00": 2500
                }
            ]
        },
        {
            "2020-01-01T00": [
                {
                    "2020-12-01T00:00:00": 2400
                },
                {
                    "2020-11-01T00:00:00": 1500
                }
            ]
        }
    ]

I don't know how to format these two years in two objects. I tried hard but as I'm new to python so please help if you know the way to format this data.

EDITED

SAMPLE CODE

result = []
for item in data_set:
    result.append({
        str(datetime.datetime(item[1], item[0], 1)): float(item[2])
    })

With above code I got the following output,

    [
        {
            "2021-02-01 00:00:00": "2300"
        },
        {
            "2021-01-01 00:00:00": "2500"
        },
        {
            "2020-12-01 00:00:00": "2400"
        },
        {
            "2020-11-01 00:00:00": "1500"
        }
    ]

But I want the records of each year separate as I described above in the EXPECTED OUTPUT.

4
  • 1
    Please explain the logic of how the output is parsed. Contrary to popular belief, stack overflow users are not blessed with psychic powers :) Commented Feb 3, 2021 at 15:03
  • is 01T00:00:00 hardcoded value into your strings? Commented Feb 3, 2021 at 15:03
  • No, its not hard coded. Each tuple in the list data_set contains (month,year,value) like (2,2021,'2300'). We can get date value by datetime.datetime(2021, 2, 1) Commented Feb 3, 2021 at 15:29
  • @MusHusKat I've updated the question. Please have a look. Commented Feb 3, 2021 at 15:42

2 Answers 2

1

This should work!

data_set = [(2, 2021, '2300'),(1, 2021, '2500'),(12, 2020, '2400'),(11, 2020, '1500')]
import datetime
final_dict = dict()
for item in data_set:

    year_date = str(datetime.datetime(item[1], 1, 1))
    full_date = str(datetime.datetime(item[1], item[0], 1))
    
    current_months_dict = final_dict.get(year_date, list())
    
    current_months_dict.append({full_date: item[2]})
    final_dict[year_date] = current_months_dict

final_list = []
for key, value in final_dict.items():
    final_list.append({key:value})
print(final_list)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your suggestion but in the EXPECTED OUTPUT section there is a list with two dict of each year. But here in your solution, final result is dict.
As in the line current_months_dict = final_dict.get(year_date, dict()). For instance, final_dict is a list then how you will cover this line? And this is my question but the way to quest is not so clear.
Is this what you want?
@UzairBinShaukat - I changed the code to make the structures list. I think it is what you want.
Thank you for the suggestion but in the last line final_list = [item for item in final_dict.items()] each of the item in dict is being appended in the list including year_date key. It is not according to the EXPECTED OUTPUT.
|
0

Using groupby from itertools

from itertools import groupby
from datetime import datetime

m=[{" ".join(map(str,elem[0:2])):int(elem[2]) for elem in list(g)} for (k,g) in groupby(data_set,lambda x: x[1])]
#grouping is done based on the year 1.e x[1] first element
[{'2 2021': 2300, '1 2021': 2500}, {'12 2020': 2400, '11 2020': 1500}]

#using datetime module to parse date as required
n=[ {datetime.strptime(k,'%m %Y').strftime("%Y-%m-%dT%H:%M:%S"):v for k,v in item.items() } for item in m ]
[{'2021-02-01T00:00:00': 2300, '2021-01-01T00:00:00': 2500},
{'2020-12-01T00:00:00': 2400, '2020-11-01T00:00:00': 1500}]

#creating nested dict using minimum of existing keys
o=[{min(item.keys()):[{k:v} for k,v in item.items()]} for item in n]
[{'2021-01-01T00:00:00': [{'2021-02-01T00:00:00': 2300},
{'2021-01-01T00:00:00': 2500}]},
{'2020-11-01T00:00:00': [{'2020-12-01T00:00:00': 2400},
{'2020-11-01T00:00:00': 1500}]}]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.