Python:list由字典项组成,如果in list也是它的值,则从list中删除key

2024-05-16 21:15:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有季节和月份的字典。你知道吗

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"]} 

我有一个程序,要求用户开放时间。用户可以把这两个季节,假期时间和月份和程序使这些列表。我的问题是,如果在这个列表中同时有key和value,那么这个值就太大了。所以如果列表中既有夏天也有六月,六月就太多了。你知道吗

如果列表是这样的:

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

六月应该删除,所以它应该是这样的:

open_time = [may, september, october, summer]

我试过:

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

应该怎么做?你知道吗


Tags: 列表timevalueopenmaylistsummerspring
3条回答

如果描述某个月的季节已经在列表中,那么听起来您需要从列表中删除该月。因为要查找给定季节,一种有效的方法是反转现有的dict并使用set而不是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

我不知道我是否能理解你想要什么,但这里有一个解释我试图做的评论尝试。你知道吗

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)

我想出了这个密码:

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'])

相关问题 更多 >