0

I have a large dictionary with time format values. the example of the dictionary is "mydict". each list([[]]) shows times of a week. Each times of a day divided with ','. For example saturday times are 06:00 - 09:30 AM - 10:30 AM - 14:00 - 17:00 - 21:00. and Sunday times are 07:00 - 09:30 AM - 11:30 AM - 14:00 - 16:00 - 21:00 in the dictionary. The dictionary involves times of 7 days and thousands key/values.

I also have a string with a specific format for 7 days. I simplified the string to 2 days. I want to replace "dayname", "endTime" and "startTime" values of the string with the nested list of the dictionary then write it in dictionary format. My ideal result is shown below.

How can i solve the issues?

mydict= {'x': [['06:00 - 09:30 AM - 10:30 AM - 14:00 - 17:00 - 21:00, 07:00 - 09:30 AM - 11:30 AM - 14:00 - 16:00 - 21:00']]}

Expected Output:

{'x': [{
    "DayName": "saturday",
    "timeList": [
        {
            
            "endTime": "06:00", 
            "startTime": "09:30"
        },
        {
            
            "endTime": "10:30", 
            "startTime": "14:00"
        },
        {
            
            "endTime": "17:00", 
            "startTime": "21:00"
        }
    ]
},

{
    "DayName": "sunday",
    "timeList": [
        {
           
            "endTime": "07:00",
            "startTime": "09:30"
        },
        {
           
            "endTime": "11:30",
            "startTime": "14:00"
        },
        {
           
            "endTime": "16:00",
            "startTime": "21:00"
        }
    ]
}]}
4
  • the dictionary you posted is incomplete post the valid input Commented Aug 6, 2020 at 8:45
  • mydict is not a valid python dictionary did you posted the correct dictionary Commented Aug 6, 2020 at 8:48
  • mydict= {'x': [['06:00 - 09:30 AM - 10:30 AM - 14:00 - 17:00 - 21:00', '07:00 - 09:30 AM - 11:30 AM - 14:00 - 16:00 - 21:00']]} is this correct? comma inside the quotes? Commented Aug 6, 2020 at 9:02
  • Yes @deadshot. It's correct Commented Aug 6, 2020 at 9:03

2 Answers 2

1

Try this:

mydict = {'x': [['06:00 - 09:30 - 10:30 - 14:00 - 17:00 - 21:00', '07:00 - 09:30 - 11:30 - 14:00 - 16:00 - 21:00']]}
k = ('endTime', 'startTime')
weeks = ['saturday', 'sunday']

dd = [[dict(zip(k, y)) for y in (z for z in zip(x.split(' - ')[::2], x.split(' - ')[1::2]))] for x in mydict['x'][0]]
res = {'x': [{'DayName': x, 'timeList': y} for x, y in zip(weeks, dd)]}

print(res)

Output:

{'x': [{'DayName': 'saturday',
        'timeList': [{'endTime': '06:00', 'startTime': '09:30'},
                     {'endTime': '10:30', 'startTime': '14:00'},
                     {'endTime': '17:00', 'startTime': '21:00'}]},
       {'DayName': 'sunday',
        'timeList': [{'endTime': '07:00', 'startTime': '09:30'},
                     {'endTime': '11:30', 'startTime': '14:00'},
                     {'endTime': '16:00', 'startTime': '21:00'}]}]}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your answer. I have another question. some lists have different times. In question example i have 3 times per days but some days have 2 or 4 times for each day.
It will work for any number. as long as each endTime has startTime
Why the result is wrong for this dictionary with your code? mydict = {'x': [['15:00 - 19:00, 00:00 - 23:59, 11:00 - 19:00, 09:00 - 20:00, 11:00 - 19:00, 10:00 - 14:00, 00:00 - 23:59']]}
you have posted input like this in question mydict = {'x': [['06:00 - 09:30 - 10:30 - 14:00 - 17:00 - 21:00', '07:00 - 09:30 - 11:30 - 14:00 - 16:00 - 21:00']]} how it will work for that input? please maintain the consistency with the input? if you give input in wrong format how it will work?
Is that a solution for 4 or 8 times for each day?
0

This is a simple approach that I don't know if it meets your requirements.

import json

x_data = json.loads(idealresult['x'])
days = []
for item in x_data:
    days.append('-'.join([f"{tm['startTime']} - {tm['endTime']}" for tm in item['timeList']]))

idealresult['x'] = json.dumps(days)

I got lost in the direction.

mydict= {'x': [['06:00 - 09:30 AM - 10:30 AM - 14:00 - 17:00 - 21:00, 07:00 - 09:30 AM - 11:30 AM - 14:00 - 16:00 - 21:00']]}
days = ['saturday', 'sunday']
x_data = mydict['x'][0][0]
results = []
for i,item in enumerate(x_data.split(', ')):
    tmp = item.split(' - ')
    results.append(
        {
            'DayName': days[i % 2], 
            'timeList': [f'{tmp[i]} - {tmp[i+1]}' for i in range(0, len(tmp), 2)]
        }
    )

idealresult = dict(x=json.dumps(results))

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.