Home > other >  How to get store multiple values from a string
How to get store multiple values from a string

Time:07-16

I am working with the yelp dataset. In that is a dictionary for the schedule of multiple restaurants.

They are sorted as shown below:

{'Friday': '8:0-23:0',
 'Monday': '8:0-22:0',
 'Saturday': '8:0-23:0',
 'Sunday': '8:0-22:0',
 'Thursday': '8:0-22:0',
 'Tuesday': '8:0-22:0',
 'Wednesday': '8:0-22:0'}

I want to get the timespan for each day by subtracting the closing time from the opening time and then get the sum of all of them. However, I am struggling to do the subtraction.

Thanks in advance.

CodePudding user response:

you can loop over the dict values and .split("-") the value to a list. This creates the two values per day. Then, remove the last two (:0) by loop in over the values in the list with my_list[value] = my_list[value][:-2].

Now you have a list with only the numbers you are looking for. cast them to int and perform the subtraction.

CodePudding user response:

dc = {'Friday': '8:0-23:0',
 'Monday': '8:0-22:0',
 'Saturday': '8:0-23:0',
 'Sunday': '8:0-22:0',
 'Thursday': '8:0-22:0',
 'Tuesday': '8:0-22:0',
 'Wednesday': '8:0-22:0'}

sum_elapsed = 0
for _, v in dc.items():
    start, end = v.split('-')
    hhs, mms = (int(v) for v in start.split(':'))
    hhe, mme = (int(v) for v in end.split(':'))
    elapsed = (hhe * 60   mme) - (hhs * 60   mms) 
    sum_elapsed  = elapsed

print(sum_elapsed)
  • Related