[英]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.