[英]Python: list consist of dictionary items, remove key from list if in list is also it's value
我有四季和幾個月的字典。
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"]}
我有一個程序向用戶詢問開放時間。 用戶可以在這里輸入季節,假日時間和月份,程序可以列出這些內容。 我的問題是,如果此列表中同時包含鍵和值,則該值過多。 因此,如果列表中同時包含夏季和6月,則6月過多。
因此,如果列表是這樣的:
open_time = [may, june, september, october, summer]
6月應刪除,因此應如下所示:
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
應該怎么做?
如果描述該月份的季節已經在列表中,這聽起來像您要從列表中刪除一個月。 因為您想在給定季節的關鍵的情況下查找月份的值 ,所以一種有效的方法是反轉您擁有的字典,並使用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
我不知道我是否能夠理解您想要的內容,但是這里有嘗試,並附有注釋以解釋我的嘗試。
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'])
如果我理解正確,您正在嘗試從字典中刪除鍵。 無需創建列表,只需在迭代時刪除鍵即可。
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.