python intersection initial state standard practice -


example:

complete_dict = dict() common_set = set() count, group_name in enumerate(groups_dict):     complete_dict[group_name] = dict()  # grabs json file = group_name.json      if count > 0:         common_set.intersection_update(complete_dict[group_name])     else:         # accounts fact common_set empty begin         common_set.update(complete_dict[group_name]) 

where: dict2 contains k:v of members(of group): int(x)

is there more appropriate way handle initial state of intersection?

i.e. cannot intersect first complete_dict[group_name] common_set because empty , result therefore also empty.

one way avoid special case initialize set contents of item. since value intersected value itself, won't lose way.

here's attempt show how work. i'm not i've understood different dict(...) calls supposed represent, may not translate code, should on right path.

it = iter(dict(...)) # dict(...) statement first_key = next(it) results[first_key] = dict(...) # dict(...) inside loop  common = set(results[first_key]) # initialize set first item  key in it:     results[key] = dict(...) # inner dict(...) again     common.intersection_update(result[key]) 

as jamylak commented, calls making keys method of various dictionaries unnecessary (a dictionary acts pretty set of keys if don't indexing or use mapping-specific methods). i've given chosen variables more in keeping python's style (lowercase regular variables, capitals reserved constants).


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -