Reputation: 65
Here's the problem I'm trying to solve:
I have boxes A
, B
, C
, and D
and have balls a
,b
,c
,d
,e
, f
, ... -- each of which is in one of the aforementioned boxes. So I know that, for example, balls a
, b
, c
and d
are in box A
. Balls e
, j
, p
, and w
are in box B
, etc.
I'm trying to color each ball based on which box contains it, so I need a data structure to keep and handle this information efficiently.
My first thought was to keep a dict like {'a':'A', 'b':'A',... 'w' : 'B' ...}
and if a
's value is A
color him (for example) red, but I'm not sure that this is the best way to keep info in this case.
Upvotes: 1
Views: 126
Reputation: 4772
What is efficient would depend on what you are doing and how often you are doing it. There isn't much information in your question to hazard a guess.
For example, it is not clear whether all balls in the same box have the same colour. If that is so, then you could assign the colour to the box rather than to the ball for maybe space efficiency; but what do you want to make efficient?
A lot of quesswork could be spared if you showed some of your code.
Upvotes: 1
Reputation: 13356
Another way can be keeping a dictionary with the boxes as keys and the set of balls as values:
boxes = { 'A' : set('a', 'b', 'c', 'd'), 'B' : set('e', 'j', 'p'), ... }
Then you can iterate over the keys and color all the balls in the corresponding set with the same color.
for box in boxes:
for ball in boxes[box]:
color(ball, box)
Upvotes: 2