Remove previous values and only return the recent values

Viewed 32

I'm trying to loop over this list:

data = [
   {
      "date":"2022-09-02 08:00:00.000",
      "endtime":"2022-09-02 12:00:00.000",
      "timebilled":"4.00",
      "projectno":"2479"
   },
   {
      "date":"2022-09-02 13:00:00.000",
      "endtime":"2022-09-02 16:00:00.000",
      "timebilled":"3.00",
      "projectno":"2696"
   },
   {
      "date":"2022-09-02 16:00:00.000",
      "endtime":"2022-09-02 17:00:00.000",
      "timebilled":"1.00",
      "projectno":"2479"
   },
   {
      "date":"2022-09-01 08:00:00.000",
      "endtime":"2022-09-01 10:00:00.000",
      "timebilled":"2.00",
      "projectno":"2696"
   },
   {
      "date":"2022-09-01 10:00:00.000",
      "endtime":"2022-09-01 12:00:00.000",
      "timebilled":"2.00",
      "projectno":"2479"
   },
   {
      "date":"2022-09-01 13:00:00.000",
      "endtime":"2022-09-01 17:00:00.000",
      "timebilled":"4.00",
      "projectno":"2479"
   },
   {
      "date":"2022-08-31 08:00:00.000",
      "endtime":"2022-08-31 16:00:00.000",
      "timebilled":"8.00",
      "projectno":"2489"
   },
 ...
]

and condense the info to make it look like this:

projectHours = {
 {  "2696":[
      {
         "2022-09-02 13:00:00.000":"3.00"
      },
      {
         "2022-09-01 10:00:00.000":"2.00"
      }
   ]
 }, {
   "2479":[
      {
         "2022-09-02 08:00:00.000":"4.00"
      },
      {
         "2022-09-02 16:00:00.000":"1.00"
      },
      {
         "2022-09-01 10:00:00.00":"2.00"
      },
      {
         "2022-09-01 13:00:00.000":"4.00"
      }
   ]
 }, {
   "2489":[
      {
         "2022-08-31 08:00:00.000":"8.00"
      }
   ]
 }
}

so far this is what I have:

info = {}
batch = {}
projectNums = ['2479', '2696', '2489'] 
for element in data:
    for projNum in projectNums:
        if element["projectno"] == projNum:
            startDate = element["date"]
            info = {
                startDate: element["timebilled"]
            }
            batch.append(info)
            project = {
                projNum: batch
            }
        print(project)

this is my result:

{
   "2696":[
      {
         "2022-09-02 13:00:00.000":"3.00"
      }
   ]
}, {
   "2696":[
      {
         "2022-09-02 13:00:00.000":"3.00"
      }
      {
         "2022-09-01 08:00:00.000":"2.00"
      }
   ]
}, {
   "2489":[
      {
         "2022-08-31 08:00:00.000":"8.00"
      }
   ]
}, {
   "2479":[
      {
         "2022-09-02 08:00:00.000":"4.00"
      }
   ]
}, {
   "2479":[
      {
         "2022-09-02 08:00:00.000":"4.00"
      },
      {
         "2022-09-02 16:00:00.000":"1.00"
      }
   ]
}, {
   "2479":[
      {
         "2022-09-02 08:00:00.000":"4.00"
      },
      {
         "2022-09-02 16:00:00.000":"1.00"
      },
      {
         "2022-09-01 08:00:00.000":"2.00"
      }
   ]
}, {
   "2479":[
      {
         "2022-09-02 08:00:00.000":"4.00"
      },
      {
         "2022-09-02 16:00:00.000":"1.00"
      },
      {
         "2022-09-01 08:00:00.000":"2.00"
      },
      {
         "2022-09-01 13:00:00.000":"4.00"
      }
   ]
}

I'd like to remove the previous key-value pairs and replace them with the most recent ones. How would I be able to do that? I've tried changing the type of collection but did not work for me. I'm pretty new to Python and any help would be appreciated.

Thank you!

1 Answers

I think you're not really using dicts properly, you're using them as structs. The most useful thing about them using the key to access them. You're hiding the key by putting them in lists, making it difficult to use effectively.

Instead of the projectHours structure you have, you might want something more like:

projectHours = {
  "2696":{
       "2022-09-02 13:00:00.000":"3.00",
       "2022-09-01 10:00:00.000":"2.00"
  }, 
  "2479": {
       "2022-09-02 08:00:00.000":"4.00",
       "2022-09-02 16:00:00.000":"1.00",
       "2022-09-01 10:00:00.00":"2.00",
       "2022-09-01 13:00:00.000":"4.00"
   ],
 "2489": {
         "2022-08-31 08:00:00.000":"8.00"
   }
}

Then your code would look like this:

info = {}
batch = {}
# Make this a set, not a list, because it makes the 'in' operation much faster.
projectNums = {'2479', '2696', '2489'}
for element in data:
    projectno = element["projectno"]
    # I think this is what you wanted:
    if projectno in projectNums:
        if projectno in batch:
            # Update info for existing project.
            info = batch[projectno]
        else:
            # New project, new empty info dict.
            info = {}

        startDate = element["date"]
        info[startDate] = element["timebilled"]
# You should be able to print it out directly at the end.
print(batch)

I haven't run it, there may be typos, and I've made a few assumptions about what you want, but I hope the helps at least.

Related