0

I'm trying to make a program that is given abbreviations of the days of the week (Mon, Tue, Wed etc.) and replaces these with the full word (Monday, Tuesday, Wednesday). The program creates a list full of strings and outputs it as a string. I have this working except for when something that isn't a day of the week is used. For example, a word that isn't Mon, Tue, Wed etc. (As seen in the code) I want to be able to filter out the unwanted word but don't know how to. Please help.

The code:

days = []

def format_days(days): 

  days = [d.replace('Mon', 'Monday') for d in days]
  days = [d.replace('Tue', 'Tuesday') for d in days]
  days = [d.replace('Wed', 'Wednesday') for d in days]
  days = [d.replace('Thu', 'Thursday') for d in days]
  days = [d.replace('Fri', 'Friday') for d in days]
  days = [d.replace('Sat', 'Saturday') for d in days]
  days = [d.replace('Sun', 'Sunday') for d in days]


  return days

answer = format_days(['Sat', 'Fun', 'Tue', 'Thu'])
print(answer)

# ['Saturday', 'Fun', 'Tuesday', 'Thursday'] <-- This is the output

# ['Saturday', 'Tuesday', 'Thursday'] <-- This is the output I want

I want to be able to filter out 'fun' from the list but the code needs to work for everything that isn't: Mon, Tue, Wed, Thu, Fri, Sat, Sun not just 'fun'

1
  • What was the problem when you tried to do it? Did you try using something some sort of if statement? Commented Mar 16, 2020 at 11:04

5 Answers 5

3

I'd suggest you a different approach for this. Start by defining a dictionary mapping the three first letters of a day to the entire name:

days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
d = {day[:3]:day for day in days_of_week}

print(d)

{'Mon': 'Monday',
 'Tue': 'Tuesday',
 'Wed': 'Wednesday',
 'Thu': 'Thursday',
 'Fri': 'Friday',
 'Sat': 'Saturday',
 'Sun': 'Sunday'}

And use a list comprehension with a conditional statement so that nothing is returned if a given case is not found in the dictionary:

[d.get(i) for i in ['Sat', 'Fun', 'Tue', 'Thu'] if i in d]
# ['Saturday', 'Tuesday', 'Thursday']
Sign up to request clarification or add additional context in comments.

4 Comments

Then why did you post it if it is the same as the (two!) existing other answers?
It was posted more or less on the same time (I tool longer since I took my time in adding explanations) @mkrieger1
Nice answer. I was lazy and just used the calendar module instead. Also not sure why you need get(i) here since you already checking if i in d. get() returns None if the key is not found by default, but that will never happen if i in d is True anyways. Correct me if wrong.
Yes you're right. Left it there cause I was thinking of doing it differently I think :) but youre right, not really necessary @road
2

You can make a dictionary of valid words. If anything other than a valid day is encountered it's ignored.

def fromat_days(days):
    valid_days={'Mon':'Monday',
                'Tue':'Tuesday',
                 ...
                'Sun':'Sunday'}
    return [valid_days[day] for day in days if day in valid_days]

3 Comments

Glad to help ;) @skribbsta
@Ch3steR I have the same question.
@RoadRunner Looks like someone's on a downvoting spree. ;)
2

Adding to others answers, you could also use the calendar functions day_name and day_abbr to zip() day abbreviations and full weekday names:

from calendar import day_abbr, day_name

day_map = dict(zip(day_abbr, day_name))

days = ['Mon', 'Wed', 'Fri', 'Sat']

print([day_map[d] for d in days if d in days])
# ['Monday', 'Wednesday', 'Friday', 'Saturday']

This is helpful because you don't have to generate the day dictionary yourself.

1 Comment

Nice answer. Never knew the calender module. +1
1

You can start with a mapping from abbreviations to full names and then only replace if the abbreviation is present in the mapping:

map_to_names = {'Mon': 'Monday', 'Tue': 'Tuesday', ..., 'Sun': 'Sunday'}
result = [map_to_names[s] for s in days if s in map_to_names]

Comments

0

you can store in a dict the mapping btw the first 3 characters and the day:

w_day = {'Mon': 'Monday',
 'Tue': 'Tuesday',
 'Wed': 'Wednesday',
 'Thu': 'Thursday',
 'Fri': 'Friday',
 'Sat': 'Saturday',
 'Sun': 'Sunday'}

def format_days(days): 
    return list(filter(None, map(w_day.get, days)))

format_days(['Sat', 'Fun', 'Tue', 'Thu'])

output:

['Saturday', 'Tuesday', 'Thursday']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.