简体   繁体   中英

Python: list consist of dictionary items, remove key from list if in list is also it's value

I have dictionary of seasons and months.

OPEN = {"march": ["spring"],"october": ["autumn"],"april": ["spring"],
  "january": ["winter"],"december": ["winter","christmast_holiday"],
  "september": ["autumn"],"july":"summer","summer_holidays"],
  "august": ["summer","summer_holidays"],"november": ["autumn"],
  "may": ["spring"],"june": ["summer"],"february": ["winter"]} 

I have a program which asks open times from user. User can put there both seasons, holiday times and months and program makes a list of these. My problem is that if in this list there are both key and value, the value is excessive. So if in list there is both summer and june, june is excessive.

So if list is like this:

open_time = [may, june, september, october, summer]

the june should delete, so it should look like this:

open_time = [may, september, october, summer]

I have tried:

    list = []
    for i in open_time:
        for key,value in OPEN:
            if value == OPEN[i]:
                list.append(v)
    open_time = open_time - list

How this should be done?

It sounds like you want to remove a month from a list if the season that describes that month is already in the list. Because you want to look up the value of month given the key of season , an efficient way to do this would be to reverse the dict you have and use a set rather than a list for open_time :

open_time = set(...)
SEASONS = {
    "winter": {"december", "january", "february"}, # Note: the value is a set
    "spring": {"march", "april", "may"},
    "summer": {"june", "july", "august"},
    "autumn": {"september", "october", "november"},
    "summer_holidays": {"july", "august"},
    "christmast_holidays": set(["december"]) # SIC from OP
}

for key, value in SEASONS:
    if key in open_time: # was the season specified in open_time?
        open_time -= value # then remove all months associated with that season

I don't know if I managed to understand what you want but here is a try with comments explaining what I tried to do.

OPEN = {"march": ["spring"],"october": ["autumn"],"april": ["spring"],
  "january": ["winter"],"december": ["winter","christmast_holiday"],
  "september": ["autumn"],"july":"summer",
  "august": ["summer","summer_holidays"],"november": ["autumn"],
  "may": ["spring"],"june": ["summer"],"february": ["winter"]} 

open_time = ["may", "june", "september", "october", "summer"]

for item in open_time:  # Loop through the open_time list (pretend item = "june")
  if item in OPEN:
    item = OPEN[item]
    if item[0] in open_time:  # Checks if the value of "june" is also in your list open_time
         open_time.remove(item[0])  # If the value is in the open_time list, remove it.
print(open_time)

I came up with this code:

MAPPING = {
    "january": ["winter"],
    "february": ["winter"],
    "march": ["spring"],
    "april": ["spring"],
    "may": ["spring"],
    "june": ["summer"],
    "july": ["summer", "summer_holidays"],
    "august": ["summer", "summer_holidays"],
    "september": ["autumn"],
    "october": ["autumn"],
    "november": ["autumn"],
    "december": ["winter", "christmas_holiday"]
}


samples = {
    'sample1': {
        'open_time': ['may', 'september', 'october', 'summer']
    },
    'sample2': {
        'open_time': ['may', 'june', 'september', 'october', 'summer'],
    },
    'sample3': {
        'open_time': ['december', 'winter'],
    }
}


def remove_duplicates(open_times):
    months = [x for x in open_times if x in MAPPING]
    seasons = [x for x in open_times if x not in months]

    final = seasons[:]
    for month in months:
        season_already_present = False
        for season in seasons:
            if season in MAPPING[month]:
                season_already_present = True
                break

        if not season_already_present:
            final.append(month)

    return final


for sample_data in samples.values():
    sample_data['open_time'] = remove_duplicates(sample_data['open_time'])

If I understand correctly you are trying to remove keys from your dictionary. Instead of creating a list, just remove the keys as you iterate.

for i in open_time:
    for key,value in OPEN:
        if value == OPEN[i]:
            open_time.pop(v)

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