0

I have two lists

the first one:

[
datetime.date(2021, 7, 1),
datetime.date(2021, 7, 2), 
datetime.date(2021, 7, 3), 
datetime.date(2021, 7, 4), 
...
...
...
datetime.date(2021, 7, 27), 
datetime.date(2021, 7, 28), 
datetime.date(2021, 7, 29), 
datetime.date(2021, 7, 30), 
datetime.date(2021, 7, 31)
]

And this is the second list:

[(
1, datetime.date(2021, 7, 1), 
3, datetime.date(2021, 6, 19), 
3, datetime.date(2021, 6, 20), 
2, datetime.date(2021, 6, 21), 
2, datetime.date(2021, 6, 22), 
3, datetime.date(2021, 6, 23), 
3, datetime.date(2021, 6, 24), 
1, datetime.date(2021, 6, 25), 
4, datetime.date(2021, 6, 26), 
3, datetime.date(2021, 6, 27), 
2, datetime.date(2021, 6, 28), 
1, datetime.date(2021, 6, 29)
)]

and I want to check which date in second list are exist in the first list. And then, if there was a common date between them, I want to put the previous value equal to it in a dictionary. for example "2021-07-01" it is common and then i want put previous value in a dictionary like this:

{
 "2021-07-01": "1",
}

But if there was a date in list 1 that was not in list 2, I want it to be zero in the dictionary in front of it :

{
  "2021-07-01": "1",
  ...
  "2021-07-02": "0",
  "2021-07-03": "0",
  "2021-07-04": "0",
  ...
  ...
  ...
}

Any Ideas? I hope I was able to get what I meant.

yousof
  • 277
  • 5
  • 15
  • I don't understand what want to do. Maybe show expected result. – furas Jul 18 '21 at 22:27
  • @furas Is it now clear or not? – yousof Jul 19 '21 at 12:05
  • you would have to convert second list to list only with dates (without row number) and use `for`-loop to check `for date from first_list: if date not in second_list: ...` – furas Jul 19 '21 at 16:03
  • @furas No, it's not the row number, It's the number of each order per day, and I need them to create dictionary. – yousof Jul 19 '21 at 16:17
  • it doesn't matter what numer it is. You have to create anothre list only with dates. – furas Jul 19 '21 at 16:46

2 Answers2

2

Are you looking for timedelta?

It is hard to help you without a piece of code and expected result

For the dates that don't exist I'd use a try/except statement

from datetime import datetime, timedelta

try:
    a = datetime(2021, 7, 1)
    b = a + timedelta(days = -1)
    c = tuple(b.timetuple()[:3])
except:
    c = (0,0,0)


print (c)

Sources: https://www.geeksforgeeks.org/python-datetime-timedelta-function/ https://stackoverflow.com/a/62582119/4173718

Rene Smit
  • 61
  • 1
  • 7
0

date_list is the first list and the orders_in_dates is the second list:

date_dict = dict()

for e in orders_in_dates:
    date_dict.setdefault(e[1], e[0])

complete_list = [(d, date_dict.get(d, 0))  for d in date_list]
yousof
  • 277
  • 5
  • 15