S. Gamgee S. Gamgee - 28 days ago 5x
Python Question

Application of Removing Items from Dictionaries

Suppose I have a dictionary that contains instances of kinematic objects. Each kinematic object has a position, velocity, etc. On each timestep update for the program, I want to check to see if two active objects (not the same object, mind you) occupy the same position in the reference frame. If they do, this would simulate a collision, the two objects involved would be destroyed, and their instances would be removed from the active objects dictionary.

dict actives{ 'missile' : object_c(x1, y1, z1),
'target' : object_c(x2, y2, z2),
'clutter' : object_c(x3, y3, z3),
... }


for key1 in self.actives.keys():
for key2 in self.actives.keys():
if not key1 == key2:
# Get Inertial Positions and Distance
Pos21 = self.actives[key2].Pos - self.actives[key1].Pos
distance = math.sqrt(sum(Pos21**2))
# If Distance <= Critical Distance
if distance <= 1.0e0
# Remove key1 and key2 from Actives
# -- This is where I need help --

I can't use
: the keys (and objects) would be removed from actives, but the
loops' conditions fail to recognize this and will encounter a KeyError. What can I do to remove these objects from actives while accessing the keys for the loop conditions?


I think Maximilian Peters had the right basic idea, but the items to be removed should be kept in a set rather than a list to avoid issues with an active key being in it multiple times.

I also had to add a fair amount of scaffolding to make it possible test the code in a context like you probably have it running in...

import math


class ObjectC(object):
    def __init__(self, x, y, z):
        self.posn = x, y, z
    def __repr__(self):
        return '{}({}, {}, {})'.format(self.__class__.__name__, *self.posn)

class Game(object):
    def remove_collisons(self):
        to_remove = set()
        for key1 in self.actives.keys():
           for key2 in self.actives.keys():
              if not key1 == key2:
                 # Calculate distance.
                 deltas = (
                     (self.actives[key2].posn[0] - self.actives[key1].posn[0])**2,
                     (self.actives[key2].posn[1] - self.actives[key1].posn[1])**2,
                     (self.actives[key2].posn[2] - self.actives[key1].posn[2])**2)
                 distance = math.sqrt(sum(deltas))
                 # Check for collision.
                 if distance <= CRITICAL_DIST:
                    to_remove |= {key1, key2}  # both objects should be removed

        print('removing: {!r}'.format(list(to_remove)))
        self.actives = {k: v for k, v in self.actives.items() if k not in to_remove}

x1, y1, z1 = 0, 1, 2
x2, y2, z2 = 1, 2, 3
x3, y3, z3 = 2, 3, 1

actives = {'missile' : ObjectC(x1, y1, z1),
           'target'  : ObjectC(x2, y2, z2),
           'clutter' : ObjectC(x3, y3, z3),
          } # ...

game = Game()
game.actives = actives


removing: ['target', 'missile']
{'clutter': ObjectC(2, 3, 1)}