2017-08-07 57 views
0

我有季节和月份字典。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"]} 

我有一个程序,要求用户打开时间。用户可以在那里放置季节,节假日和月份,程序会列出这些列表。我的问题是,如果在此列表中同时存在关键字和值,则该值过大。所以如果在名单上有夏天和六月,六月是过度的。

所以,如果名单是这样的:

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 

这应该怎么做NE?

+4

如果你打算去制造假数据的麻烦,**至少使它成为一个有效的Python文字**。 –

+0

在你的例子中,第一个'open_time'与第二个是相同的......第一个应该读取'open_time = [may,6月,9月,10月,夏天]'吗? – PaSTE

+0

你可以说什么第一次open_time举行,然后需要删除什么?现在,它很难遵循这个问题 – Tammy

回答

-1

如果我理解正确,您试图从字典中删除键。您不必创建列表,只需在迭代时删除键。

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

任何人都在意解释downvote? – tnknepp

0

听起来好像你想从列表中删除一个月,如果描述该月份的季节已经在列表中。因为你要查找个月给出赛季中的关键,一个有效的方式做到这将是扭转你的字典,并使用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 
0

我不知道我是否设法理解你想要什么,但这里是一个试着解释我试图做什么的评论。

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) 
0

我想出了这个代码:

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']) 
相关问题