'remove keys in a dictionary if a value in its dictionary field is a subset
I need to get the cat_n with maximum matches and remove cat_m if it is a subset. For example
vehicle_dict = {"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "auto"]}}
should return
{"cat_1":{"type":["car", "bus", "auto"]}}
but
vehicle_dict = {"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "train"]}}
should return both i.e
{"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "train"]}} as none of them are a subset.
So for
vehicle_dict = {"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "auto"]}, "cat_3": {"type":["car", "train"]}}
it should return
{"cat_1":{"type":["car", "bus", "auto"]}, "cat_2": {"type":["car", "train"]}}
Solution 1:[1]
I assume that the goal is to filter the vehicle_dict as follows: For each category cat_x in sequence, exclude the category if all its types have already been seen in previous categories.
The code:
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']}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
