Adding / rewriting to a dictionary depending on elements
py 2.7. I have a dictionary of lists. Each cycle of each list is overwritten with a new updated version.
I am using a third party particle system. What I am doing is that each key is the index number of the particles, and the list is its position and previous positions.
But when a particle "dies", the indices of the particles are all shifted. The key will be overwritten incorrectly when a new particle is born with the same index as the dead particle. I want to store this key with the positions of the dead particle.
Here's the code as it is now:
if frame == 0:
branches = {}
...
for p in xrange(particle_count):
xp = emitter.GetParticle(p) #xp = current particle
trail = []
index = xp.GetIndex()
trail_length = xp.GetCustomDataCount() #number of previous positions
for i in xrange(trail_length):
previous_position = xp.GetCustomData(i)
trail.append(previous_position)
branches [index] = trail
I was thinking about comparing the first element of each list with the first element of the list that it is trying to overwrite. Then if it is different, add 1 to the index number until there is free space ...?
EDIT - I've made further progress and figured out what I need to do, but don't know python. Here are some new codes:
for p in xrange(particle_count):
xp = emitter.GetParticle(p) #xp = current particle
trail = []
index = xp.GetIndex()
trail_length = xp.GetCustomDataCount()
for i in xrange(trail_length):
previous_position = xp.GetCustomData(i)
trail.append(previous_position)
if index in branches:
this_trail = trail[0]
set_trail = branches[index]
set_trail = set_trail[0]
if this_trail == set_trail:
branches[index] = trail
else:
for b in branches:
set_trail = branches[b]
set_trail = set_trail[0]
if this_trail == set_trail:
branches[index] = trail
break
else:
branches[index] = trail
Problem: when I say "if the index is in the branches". I check every entry for consistency. If the trails are the same, the old one is overwritten with the new one. However, if the index exists in the dictionary but does not match the entry, nothing happens. This is what I need:
if index in branches:
this_trail = trail[0]
set_trail = branches[index]
set_trail = set_trail[0]
if this_trail == set_trail:
branches[index] = trail
else:
check all entries for a match(like i currently do)
if match, overwrite entry
if no match, add entry to a non-existing key
else:
branches[index] = trail
source to share
Ok I think I'm getting your problem, your code assumes the dictionaries are ordered, but they are not, they are in arbitrary order, and the actual order really depends on your dictionary's insertion and deletion history, as well as python specific implementation.
You do not have to depend on the ordered dictionary, if you want to order in your dictionary, you can try using collections.OrderedDict
.
They are like regular dictionaries, except that they preserve the order of the elements in them. Example -
>>> from collections import OrderedDict
>>> d = OrderedDict()
>>> d[1] = 2
>>> d[5] = 10
>>> d[2] = 11
>>>
>>> d
OrderedDict([(1, 2), (5, 10), (2, 11)])
Though you might want to rethink if the dictionary is the actual data structure you want to use. If your indices are equal numbers, you are better off using a simple list. If they are tuples of coordinates (x,y)
, you can use a 2-dimensional list for that.
source to share