Home > Software engineering >  how to set dictionary if select key is repeats with python?
how to set dictionary if select key is repeats with python?

Time:11-03

I'm using python 3 and I have a dictionary containing some scheduler data. Now i want to set that if a day name repeats in the data list.

here is the case and code :

data = [
    {'day': 'Monday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Tuesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Wednesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Thursday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Friday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Saturday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
    {'day': 'Saturday', 'full_day': False, 'close_day': True, 'start_time': None, 'close_time': None},
    {'day': 'Sunday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
]

as you can see Saturday is 2 times in data list. second one is full day false and close day true . Now i want that to stay there and first one Saturday object remove from the list. And this thing with all days which comes in list multiple times.

CodePudding user response:

You can use a dictionary with day as the key for this (using dictionary comprehension):

data = list({d['day']: d for d in data}.values())

This will keep only the last occurrence of each day.

The above is equivalent to:

'''
data_dict will be:
{
   'Monday': {'day': 'Monday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
   'Tuesday': {'day': 'Tuesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
   ...
}
'''
data_dict = {}

for d in data:
    data_dict[d['day']] = d

data = list(data.values())

data_dict is a dictionary of day:data. So for each day, data_dict will store its latest entry from data (as older entries will be overridden).

CodePudding user response:

for-loop will find a saturday you don't need and pop it

data = [
{ 'day': 'Monday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{ 'day': 'Tuesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{ 'day': 'Wednesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{ 'day': 'Thursday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{ 'day': 'Friday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{'day': 'Saturday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None},
{ 'day': 'Saturday', 'full_day': False, 'close_day': True, 'start_time': None, 'close_time': None},
{'day': 'Sunday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None} 
]

for e,i in enumerate(data):
    if i['day'] == 'Saturday' and i['full_day']:
        data.pop(e)

for i in data:
    print(i)

# {'day': 'Monday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
# {'day': 'Tuesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
# {'day': 'Wednesday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
# {'day': 'Thursday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
# {'day': 'Friday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
# {'day': 'Saturday', 'full_day': False, 'close_day': True, 'start_time': None, 'close_time': None}
# {'day': 'Sunday', 'full_day': True, 'close_day': False, 'start_time': None, 'close_time': None}
  • Related