Reputation: 2054
Example:
complete_dict = dict()
common_set = set()
for count, group_name in enumerate(groups_dict):
complete_dict[group_name] = dict() # grabs a json file = group_name.json
if count > 0:
common_set.intersection_update(complete_dict[group_name])
else:
# this accounts for the fact common_set is EMPTY to begin with
common_set.update(complete_dict[group_name])
WHERE: dict2
contains k:v of members(of the group): int(x)
Is there a more appropriate way to handle the initial state of the intersection?
I.e. We cannot intersect the first complete_dict[group_name]
with common_set
because it is empty and the result would therefore also be empty.
Upvotes: 1
Views: 118
Reputation: 1887
Logically when we want to assign the output of intersection between several sets, we assign the smallest set (set with minimum length) to the output set and compare other sets with it right?
uncommon_result = dict(...) # a method that returns a dict()
common_set=sorted([(len(x),x) for x in uncommon_result.values()])[0][1]
for a_set in uncommon_result.values():
common_set.intersection_update(a_set)
I know the second line can be one of the worst thing to do to initiate COMMON_SET
cause it does lots of unnecessary works but mathematically almost all the time we want to know which one of our sets is the smallest, in that case, these works aren't in vain.
EDIT: If uncommon_result is a dict of dicts, you may need to add another for loop to go over its keys and with some minor changes in the above inner for, you'll be good again.
Upvotes: 0
Reputation: 43527
Following Blcknght's answer but with less complexity and repetition:
common = None
uncommon = {}
for key in outer_dict:
inner = uncommon[key] = inner_dict(...)
if common is None:
common = set(inner)
else:
common.intersection_update(inner)
Like Blcknght, it is hard to know if this captures the intent of the original because the variable names are not descriptive nor distinct.
Upvotes: 1
Reputation: 104792
One way to avoid the special case would be to initialize the set with the contents of the item. Since a value intersected with itself is the value itself, you won't lose anything this way.
Here's an attempt to show how this could work. I'm not certain that I've understood what your different dict(...)
calls are supposed to represent, so this may not translate perfectly into your code, but it should get you on the right path.
it = iter(dict(...)) # this is the dict(...) from the for statement
first_key = next(it)
results[first_key] = dict(...) # this is the dict(...) from inside the loop
common = set(results[first_key]) # initialize the set with the first item
for key in it:
results[key] = dict(...) # the inner dict(...) again
common.intersection_update(result[key])
As jamylak commented, the calls you were making to the keys
method of various dictionaries were always unnecessary (a dictionary acts pretty much like a set
of keys if you don't do any indexing or use mapping-specific methods). I've also given chosen variables that are more in keeping with Python's style (lowercase
for regular variables, with CAPITALS
reserved for constants).
Upvotes: 2