我有季节和月份字典。
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]
june应该删除,所以它应该是这样的:
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
应如何做到这一点?
答案 0 :(得分: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
答案 1 :(得分: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)
答案 2 :(得分: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'])
答案 3 :(得分:-1)
如果我理解正确,您正试图从字典中删除密钥。而不是创建列表,只需在迭代时删除键。
for i in open_time:
for key,value in OPEN:
if value == OPEN[i]:
open_time.pop(v)