Reputation: 79
py 2.7. I have a dictionary of lists. Each cycle each list is overwritten with a new updated version of itself.
I'm using a third party particle system. What I'm doing is having each key as the particle's index number and the list is of it's position and previous positions.
But when a particle 'dies', the indexes of the particles all get shifted. A key will be overwritten incorrectly when a new particle is born with the same index as the dead particle. I want to preserve that key with 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 of comparing the first element of each list with the first element of the list that it's trying to overwrite. Then if it's different, add 1 to the index number until there's a free spot...?
EDIT - I've made further progress and have identified what I need to do, but don't know the python. Here's some new code:
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
The problem: When I say "if index in branches.." I'm checking each entry for a match. If the trails are the same, the old is overwritten with the new. However, if the index does exist in the dictionary, but is not the same as the entry, nothing will happen. Here's 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
Upvotes: 3
Views: 81
Reputation: 90989
Okay, I think I get your issue, your code is assuming that Dictionaries are ordered , but they are not, they have arbitrary order, and the actual order really depends on the insertion and deletion history of your dictionary as well as the specific python implementation.
You should not depend on your dictionary being ordered , if you want order in your dictionary, you can try using collections.OrderedDict
.
They are similar to normal dictionaries, with the exception 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 may want to rethink whether dictionary is the actual data structure that you want to use. If your indexes are plain numbers, you are better off using a simple list . If they are a tuple of (x,y)
coordinates, you can use a 2 dimensional list for that.
Upvotes: 1