0

I have 2 lists of dicts:

hah = [{1:[datetime(2014, 6, 26, 13, 7, 27), datetime(2014, 7, 26, 13, 7,27)]},
                  {2:datetime(2014, 5, 26,13,7,27)}]
dateofstart = [{1:datetime(2013, 6, 26, 13, 7, 27)}, {2:datetime(2013, 6, 
                  26,13,7,27)}]` (just examples).

And I need to create a new list of dicts that contains the same keys and as a value has a difference between dates from the hah and dateofstart.

It should look like this:

[{1:[365, 334]}, {2:[390]}]

When i tried to do it myself i got this code

dif = list()
diff = list()
for start in dateofstart:
    for time in hah:
        if start.keys() == time.keys():
            starttime = start.values()
            timeofpay = time.values()
            for payments in timeofpay:
                dif.append(starttime.pop(0) - payments)
            diff.append({str(start.keys()):str(dif)})

And it runs with a following error:

Traceback (most recent call last):
  File "/Users/mihailbasmanov/Documents/date.py", line 78, in <module>
    dif.append(starttime.pop(0) - payments)
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'list'

Edition:

Managed to do it (almost) by myself. Here is resulted code:

for start in dateofstartbuyer:
    for time in hah:
        if start.keys() == time.keys():
            starttime = start.values()
            starttimetime = starttime.pop(0)
            timeofpay = time.values()
            for payments in timeofpay:
                if type(payments) == list:
                    for wtf in payments:
                        dif.append(wtf - starttimetime)
                else:
                    dif.append(payments - starttimetime)
            key = str(start.keys())
            diff.append({key[1:2]:str(dif)})
            dif = list()
print(diff)

If you have a suggestion how to make this code more productive your welcome to post your suggestions in the comments or in answers.

M.Basman
  • 1
  • 8
  • Please make sure that the code you posts actually runs. Right now, your second block contains several variables of which we have no idea what they contain. – acdr Aug 05 '16 at 09:30
  • I would strongly advise that you simplify the packaging of your data. You have a `list` of individual `dict`s of `list`s. That's a mindful. Try and design from the user side: how would the user use this data? – polarise Aug 05 '16 at 09:31
  • @polarise User must be able to see all differences of each user he/she wants to get so I thought that is not a bad idea to create a list where is possible to find a user by his id. – M.Basman Aug 05 '16 at 09:41
  • Possible duplicate of [Difference between two dates?](http://stackoverflow.com/questions/8419564/difference-between-two-dates) – tripleee Aug 05 '16 at 09:47
  • 1
    Not a duplicate but the title is misleading. It's more about structuring the result. – polarise Aug 05 '16 at 10:07
  • @polarise Yap, you right. And I think I made it with help of the question tripleee sanded. – M.Basman Aug 05 '16 at 10:42
  • This list of `dicts` doesn't makes sense to me. I think these are supposed to be normal dictionaries. Is it? Because, the example that you provided is list of `dict` with just one key in each dictionary. This is not how `dict` works. All the `key` should belong to single `dict` object.. – Moinuddin Quadri Aug 24 '16 at 09:17

1 Answers1

0

I am assuming that the example that you provided is having typo issues, because:

  • hah is a list of dict with just one key in each dictionary. Instead it is supposed to be single dictionary. Isn't? Also, for key 2, value is single datetime() object instead of list. Isn't it suppossed to be [datetime]?

    hah = [{1:[datetime(2014, 6, 26, 13, 7, 27), datetime(2014, 7, 26, 13, 7,27)]}, {2:datetime(2014, 5, 26,13,7,27)}]

Hence, I am assuming hah and dateofstart as dict objects. Below is the code. Let me know in case any of my assumption is incorrect.

>>> hah = {1:[datetime(2014, 6, 26, 13, 7, 27), datetime(2014, 7, 26, 13, 7,27)],
...        2:[datetime(2014, 5, 26,13,7,27)]}
>>> dateofstart = {1:datetime(2013, 6, 26, 13, 7, 27), 2:datetime(2013, 6,26,13,7,27)}
>>> dicts = {}
>>> for key, values in hah.items():
...     dicts[key] = [(value - dateofstart[key]).days for value in values]
...
>>> dicts
{1: [365, 395], 2: [334]}
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • Sorry, but i did it myself long time ago, and then rewrote it so now I even can't find place where I had this trouble so I can't check your solution. Sorry again. – M.Basman Aug 29 '16 at 08:42