简体   繁体   English

Python:列表由字典项组成,如果列表中的值也从列表中删除键

[英]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. 因此,如果列表中同时包含夏季和6月,则6月过多。

So if list is like this: 因此,如果列表是这样的:

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

the june should delete, so it should look like this: 6月应删除,因此应如下所示:

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 : 因为您想在给定季节关键的情况下查找月份 ,所以一种有效的方法是反转您拥有的字典,并使用set而不是list作为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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对于列表中的每个值,如何从字典列表中删除项目 - How to remove items from dictionary list, for each value that's in a list python-如果字典键等于某个值,则从列表中删除字典 - python - remove a dictionary from a list if dictionary key equals a certain value Python从项目列表创建字典键 - Python creating dictionary key from a list of items Python:如果列表项是字典中给定值的键,则将其删除 - Python: Remove list item if it is key for a given value from dictionary 如何使用python从列表中获取字典,将列表条目作为键,并将列表中的项数作为值? - How do I obtain a dictionary from a list, with list entrys as key and the amount of the items in the list as value, using python? Python-如果字符串列表不在字典键中,则从字典列表中删除字典 - Python - Remove dictionary from list of dictionaries if list of strings not in dictionary key 列出 Python 中的字典以按键值分组 - List dictionary in Python to group by in key's value Python按键删除列表中的字典 - Python remove dictionary in a list by key 形成一个字典,密钥来自列表,value是Python中另一个列表中项目的出现 - Form a dictionary that key is from a list, value is the occurrence of items in another list in Python 如果项目中的值重复,如何从字典列表中删除字典 - How to remove dictionary from list of dictionary if value repeats in items
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM