Home > Net >  flatten out sub level data of dictionary into one level?
flatten out sub level data of dictionary into one level?

Time:03-17

"dataFrameData": [
            {
               "intersection": {
                  "Item": "Item1",
                  "Customer": "Customer1",
                  "Month": "1"
               },
               "measures": {
                  "Sales": 1212.33,
                  "Forecast": 400
               }
            },
            {
               "intersection": {
                  "Item": "Item1",
                  "Customer": "Customer1",
                  "Month": "2"
               },
               "measures": {
                  "Sales": 1200,
                  "Forecast": 450
               }
            }
         ]

I have dataframe stored like this in a list and want to flatten out into one level by removing "intersection" and "measures" level. after flattening it out it should look like this:

[
{
   "Item": "Item1",
   "Customer": "Customer1",
   "Month": "1"
   "Sales": 1212.33,
   "Forecast": 400
},
{
   "Item": "Item2",
   "Customer": "Customer2",
   "Month": "12"
   "Sales": 1212.33,
   "Forecast": 800
} 
]
 

Is there any approach to do that in o(1) space complexity? instead of building new list and copying items using loop

CodePudding user response:

Try this to make your new list:

original_list = [
            {
               "intersection": {
                  "Item": "Item1",
                  "Customer": "Customer1",
                  "Month": "1"
               },
               "measures": {
                  "Sales": 1212.33,
                  "Forecast": 400
               }
            },
            {
               "intersection": {
                  "Item": "Item1",
                  "Customer": "Customer1",
                  "Month": "2"
               },
               "measures": {
                  "Sales": 1200,
                  "Forecast": 450
               }
            }
         ]
print([each_element["intersection"] for each_element in original_list])

The output:

[{'Item': 'Item1', 'Customer': 'Customer1', 'Month': '1'}, {'Item': 'Item1', 'Customer': 'Customer1', 'Month': '2'}]

CodePudding user response:

Depends on what you mean by O(1). If you just want to avoid using an explicit loop, you can do this dict = [dict(x['intersection'],**x['measures']) for x in dataFrameData], which returns

[{'Item': 'Item1',
  'Customer': 'Customer1',
  'Month': '1',
  'Sales': 1212.33,
  'Forecast': 400},
 {'Item': 'Item1',
  'Customer': 'Customer1',
  'Month': '2',
  'Sales': 1200,
  'Forecast': 450}]

If you really need the space complexity, you can use [(x.update(x['intersection']),x.update(x['measures']),x.pop('intersection'),x.pop('measures')) for x in dataFrameData]. While this is list comprehension and is technically a loop, every function in there is in-place. This gives me the output:

[{'Item': 'Item1',
  'Customer': 'Customer1',
  'Month': '1',
  'Sales': 1212.33,
  'Forecast': 400},
 {'Item': 'Item1',
  'Customer': 'Customer1',
  'Month': '2',
  'Sales': 1200,
  'Forecast': 450}]
  • Related