Finding mentioned weekdays from text
Question:
Let’s say we have a text of
Even though we celebrate Good Friday and Easter Sunday, there is no mention of days such as "Sunday" or "Wednesday".
Notice the following weekdays are mentioned, Friday, Sunday, Sunday, Wednesday
We need output some variable weekdays
that, in this case, would have:
weekdays: [3,5,7]
Assuming,
- We count from 1
- We don’t care about repeated entries
- 1 is Monday
- 7 is Sunday
What would be the most Pythonic way to approach such information?
Answers:
I would use a regular expression to identify the days of the week that appear in the text, and then use a set comprehension to get the desired indices:
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
day_indices = dict(zip(days_of_week, range(1, 8)))
expr = "|".join(rf"b{day}b" for day in days_of_week)
matches = re.findall(expr, text)
result = sorted(list(set(day_indices[day] for day in matches)))
print(result)
This outputs:
[3, 5, 7]
You can create a dictionary of days and then check for keys by the words in the given text
# Dictonary with days as keys and numbers as values
day_dict = {"Monday": 1, "Tuesday": 2, "Wednesday":3 }
text = "Even though we celebrate Good Friday and Easter Sunday, there is no mention of days such as 'Sunday' or 'Wednesday' in the Bible."
res=[]
# loop over words in text
for word in text.split(' '):
# get dict value based on word
a = day_dict.get(word)
# if word exists in dict and is not yet in your "result" --> add it
if a and a not in res:
res.append(a)
Let’s say we have a text of
Even though we celebrate Good Friday and Easter Sunday, there is no mention of days such as "Sunday" or "Wednesday".
Notice the following weekdays are mentioned, Friday, Sunday, Sunday, Wednesday
We need output some variable weekdays
that, in this case, would have:
weekdays: [3,5,7]
Assuming,
- We count from 1
- We don’t care about repeated entries
- 1 is Monday
- 7 is Sunday
What would be the most Pythonic way to approach such information?
I would use a regular expression to identify the days of the week that appear in the text, and then use a set comprehension to get the desired indices:
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
day_indices = dict(zip(days_of_week, range(1, 8)))
expr = "|".join(rf"b{day}b" for day in days_of_week)
matches = re.findall(expr, text)
result = sorted(list(set(day_indices[day] for day in matches)))
print(result)
This outputs:
[3, 5, 7]
You can create a dictionary of days and then check for keys by the words in the given text
# Dictonary with days as keys and numbers as values
day_dict = {"Monday": 1, "Tuesday": 2, "Wednesday":3 }
text = "Even though we celebrate Good Friday and Easter Sunday, there is no mention of days such as 'Sunday' or 'Wednesday' in the Bible."
res=[]
# loop over words in text
for word in text.split(' '):
# get dict value based on word
a = day_dict.get(word)
# if word exists in dict and is not yet in your "result" --> add it
if a and a not in res:
res.append(a)