Reputation: 79
Here's my issue , hope someone can help I have what I call a global dictionary which in my scenario is a dictionary of all the possible key:values
in my program
global_dict = dict(zip(legend_values_global, legend_colors_global))
and the values look something like this
{1: (191, 0, 0), 2: (191, 176, 0), 3: (29, 191, 0), 5: (0, 191, 147), 6: (0, 58, 191), etc
but now I need to make a dictionary of only the values that I need for a specific list. So if like X
has only the values 1
2
and 3
then I want a new dictionary that looks like
{1: (191, 0, 0), 2: (191, 176, 0), 3: (29, 191, 0)}
I do have a list called uniqueBins
which has a list of tuples of the possible keys for each iteration
below is what I tried but I keep getting a Key Error
legend_colors_global = create_colors(len(legend_values_global))
for bracket in bins:
uniqueBins.append(tuple(sorted(set(bracket))))
global_dict = dict(zip(legend_values_global, legend_colors_global))
for i in range(waferNum):
this_wafers_legend = {k: global_dict[k] for k in uniqueBins}
The format of uniqueBins
is as follows:
[(1, 2, 3) ,(5, 10, 11), (1,7,8)]
The line this_wafers_legend = {k: global_dict[k] for k in uniqueBins}
is the one failing but I'm not sure why. Any suggestions would be greatly appreciated.
Upvotes: 0
Views: 38
Reputation: 4606
The problem is here :
{k: global_dict[k] for k in uniqueBins}
uniqueBins
is [(1, 2, 3), (5, 10, 11), (1, 7, 8)]
so k for k in uniqueBins
is an entire tuple
which ends up being {(1, 2, 3): global_dict[(1, 2, 3)]}
. You can just create your dictionary using k for k in uniqueBins[0]
if your target is keys 1, 2, 3
uniqueBins = [(1, 2, 3), (5, 6, 2), (1, 3, 6)] # modified for provided data
this_wafers_legends = []
for i in uniqueBins:
this_wafers_legends.append({k : global_dict[k] for k in i})
print(this_wafers_legends)
# [{1: (191, 0, 0), 2: (191, 176, 0), 3: (29, 191, 0)}, {5: (0, 191, 147), 6: (0, 58, 191), 2: (191, 176, 0)}, {1: (191, 0, 0), 3: (29, 191, 0), 6: (0, 58, 191)}]
Upvotes: 1