简体   繁体   中英

How do I check if some values in list/tuple is equal?

I am trying to print out the operating hours of a stall. I'm trying to check if all the values of operating hours are the same, I should be printing

Monday to Sunday: 0800 - 2200

Else it should break into the different operating hours.

Monday to Friday: 0800 - 2200
Saturday to Sunday: 1100 - 2000

The values of the list are created depending on the stall. As an example, one of the stalls has operating hours of such.

operating_hours_list = [['MONDAY', '0800 - 2200'], ['TUESDAY', '0800 - 2200'], ['WEDNESDAY', '0800 - 2200'], ['THURSDAY', '0800 - 2200'], ['FRIDAY', '0800 - 2200'], ['SATURDAY', '1100 - 2000'], ['SUNDAY', '1100 - 2000']]

Thank you!

You can use itertools.groupby() to group the list by consecutive elements which have the same hours (index 1 of the nested list):

import itertools

operating_hours_list = [['MONDAY', '0800 - 2200'], ['TUESDAY', '0800 - 2200'], ['WEDNESDAY', '0800 - 2200'], ['THURSDAY', '0800 - 2200'], ['FRIDAY', '0800 - 2200'], ['SATURDAY', '1100 - 2000'], ['SUNDAY', '1100 - 2000']]
groups = itertools.groupby(operating_hours_list, lambda x: x[1])
# groups looks like
# [('0800 - 2200', <itertools._grouper object at 0x1174c8470>), ('1100 - 2000', <itertools._grouper object at 0x1176f3fd0>)]
# where each <itertools._grouper> object contains elements in the original list
for hours, days in groups:
    day_list = list(days)
    # if there's only one unique day with these hours, then just print that day
    # e.g. day_list = [['MONDAY', '0800 - 2200']], so we need to take the first element of the first element
    # we additionally call .title() on it to turn it to 'Title Case' instead of all-caps
    if len(day_list) == 1:
        print("{}: {}".format(day_list[0][0].title(), hours))
    # otherwise, we get both the first day with these hours (index 0), 
    # and the last day with these hours (index -1).
    else:
        print("{} to {}: {}".format(day_list[0][0].title(), day_list[-1][0].title(), hours))

This outputs:

Monday to Friday: 0800 - 2200
Saturday to Sunday: 1100 - 2000

Since you're looking for guidance I'll help you with some pseudo-code (as giving you the answer won't be helpful for learning)

First you'll need something to store the set of open times. This could be one or multiple sets, and each set will have a range of days and a time. Therefore a 2D list is an option:

openings = [[]]

We know that we can start with Monday and its time and go until we find a day with a different time or reach the end of the week. So we can start it as such:

openings = [[operating_hours_list[0][0], operating_hours_list[0][1]]

This will start us with

[['Monday', '0800 - 2200']]

Now loop from the next day until you find a different time (this is now pseudo-code - try translating to Python)

for d from 1 to 6: #Tuesday to Sunday indices
    if operating_hours_list[d][1] == openings[end][1]:
        keep going
    else: # found a day with a different time
        append (' to ' + operating_hours_list[d-1][0]) to openings[end][0]
        append a new opening set to openings with day and times from opening_hours_list

Now this will get you started, and then think about how you'll handle when you get to the end of the week. Keep in mind you can index the last item in a list with [-1] , and do it in steps and run and test so you can fix problems as they come up. Hope this helps and you learn a bit from this problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM