Python Forum

Full Version: corresponding lists
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have to lists the idea is I'm going to get the day with calendar and datetime lets say for example that today is Wednesday I would like Wednesday to correspond with the numbers list but Wednesday would start at 0 in the numbers list and the rest would follow. that way if I have Tuesday in my text it would index 1. So basically when I grab the day of the week from calendar and datetime I always want the current day to start at 0 on the numbers list. Any help would be greatly appreciated Thank you!
def get_day(text):
    days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    number = [0, 1, 2, 3, 4, 5, 6]
    return number[days.index(text)]


text = "Wednesday"

print(get_day(text))
I'm not following exactly how you're keeping track of the different parts. You've already got how to go from "Wednesday" to 3.

Can you show an example of what you want to happen next? If you want to restart with Wednesday at 0, I don't understand how Tuesday would then become 1.

I was assuming that you'd just use an offset and a mod 7 to generate the new index, but that would make Tuesday 6, not 1.
i got it this is was what i was trying to do

def get_day(text):
    days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
    for i in days:
        if i in text:
            return str(i)


def day_to_number(text):
    if "next" in text:
        next_ = + 7
    else:
        next_ = + 0
    my_date = datetime.datetime.today()
    today = calendar.day_name[my_date.weekday()]
    dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
                'Friday', 'Saturday', ]
    startDayName = today
    startIndex = dayNames.index(startDayName)
    rotatedDayNames = dayNames[startIndex:] + dayNames[:startIndex]
    number = [0, 1, 2, 3, 4, 5, 6]
    text = get_day(text)
    day_num = number[rotatedDayNames.index(text)]
    next_or_else = day_num + next_
    return next_or_else


def suffix(d):
    return 'th' if 11 <= d <= 13 else {1: 'st', 2: 'nd', 3: 'rd'}.get(d % 10, 'th')


def custom_strftime(format, t):
    return t.strftime(format).replace('{S}', str(t.day) + suffix(t.day))


def day_to_date():
    return custom_strftime('%B {S} %Y', datetime.datetime.now() + datetime.timedelta(days=day_to_number(text)))


text = "this Wednesday"
print(get_day(text))
print(day_to_number(text))
print(day_to_date())

text = "next Wednesday"
print(get_day(text))
print(day_to_number(text))
print(day_to_date())