2017-08-07 53 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']) 
相關問題