[英]remove keys in a dictionary if a value in its dictionary field is a subset
我需要獲得具有最大匹配項的cat_n ,如果它是一個子集,則刪除cat_m 。 例如
vehicle_dict = {"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "auto"]}}
應該返回
{"cat_1":{"type":["car", "bus", "auto"]}}
但
vehicle_dict = {"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "train"]}}
應該同時返回
{"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "train"]}}
因為它們都不是一個子集。
因此對於
vehicle_dict = {"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "auto"]}, "cat_3": {"type":["car", "train"]}}
它應該返回
{"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "train"]}}
我假設目標是按如下方式過濾 vehicle_dict:對於序列中的每個類別 cat_x,如果該類別的所有類型都已在之前的類別中出現,則排除該類別。
代碼:
def filter(vehicle_dict):
result = {}
all = set() # Set of all types types encountered so far
i = 0 # Categoey number in the result
for cat in sorted(vehicle_dict.keys()):
types = vehicle_dict[cat]['type'] # Get the category types
typeset = set(types) # Make a set out of the types,
# If and only if the category contains new type(s) it is included in the result
if not typeset.issubset(all):
i += 1
result[f'cat_{i}'] = vehicle_dict[cat] # Include the category in the resul
t
all = all.union(typeset) # Keep track of all types encountered so far.
return result
vehicle_dict = {"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "auto"]}}
print(filter(vehicle_dict))
vehicle_dict = {"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "auto"]}, "cat_3": {"type":["car", "train"]}}
print(filter(vehicle_dict))
Output
{'cat_1': {'type': ['car', 'bus', 'auto']}}
{'cat_1': {'type': ['car', 'bus', 'auto']}, 'cat_2': {'type': ['car', 'train']}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.