Question: I have two columns startDate and endDate
I need to calculate number of hours count from 0 to 23 between these dates
Example, start date is 2000-12-05 10:00:00 and end date is 2001-01-15 15:00:00
I need to calculate how many times hour 0 to 23 occurred between these two dates in python

I took the difference between the dates and calculated hours from the difference.
After which I plan to extract start hour from startDate till startDateHour \* hours to get the endHour
and iterate through a dictionary to increase the count, but is there any other approach with which I can do this?

```
df['diff'] = df['endDate'] - df['startDate']
df['hours']= df['diff'] / np.timedelta64(1, 'h')

```

Answer: 
You can turn your expression into a regex and try matching with that:

```
import re

words = [
    'Apple',
    'Christmas Tree',
    'Shopping Bag'
]

match = '???i???as ?r??'
regex = '^' + match.replace('?', '.') + '$'  # turn your expression into a proper regex

for word in words:    # go through each word
    if re.match(regex, word):   # does the word match the regex?
        print(word)

```

Output:

```
Christmas Tree

```
If you have a *small* word list, then you can run a regex scan on the whole word list. Convert the query string into a regex and you're good to go.

Otherwise, you can organize the word list in chunks, e.g. the equivalent of splitting it into several files in different directories:

```
Appl => [ Apple ]
Chri => [ Christchurch, Christmas Tree, ... ]
Shop => [ Shopper, Shopping Bag, Shopkeeper, ]

```

(You can have several levels).

Since your search query seems to be *anchored*, i.e. you know it starts at the word's beginning, when you look for "???i???as ?r??" you see that "???i" will only match "Chri", and only look in that sub-list.

(Actually if you have to do this a lot of times, you'll better build a recursive search and organize the list as a [n- or "m-ary" tree](https://en.wikipedia.org/wiki/M-ary_tree) - here are some [examples](https://www.ics.uci.edu/~brgallar/week6_3.html)).